user172902
user172902

Reputation: 3601

Hide navigation bar but also keep background color of status bar

I am hiding my navigation bar using the default ios method

self.navigationController?.hidesBarsOnSwipe = true

I have a custom green color on my Navigation bar with translucent set to true. However, when the navigation bar hides, the whole custom green color including the color behind the status bar hides as shown in the images below. I want to keep the color behind the status bar the same (With the same translucent navigation bar) even when the navigation bar is hidden so that the table's content does not overlap the status bar.

status bar

I tried setting the status bar color manually using the following function. But that does not work well as my navigation bar is translucent and this only seems like a patch fix because The navigation bar is not translucent anymore. And hiding/Adding background to the status bar sometimes creates a poor user experience when transitioning VCs

    func setStatusBarBackgroundColor(color: UIColor) {
    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }
    statusBar.backgroundColor = color
}

Upvotes: 0

Views: 1453

Answers (1)

Dupinder kaur
Dupinder kaur

Reputation: 198

You can achieve this by applying gradient layer .Here is the code please have a look .

let colorTop =  UIColor.redColor().CGColor //your status bar color
   let colorBottom = UIColor.whiteColor().CGColor//color for your navigation bar
    let gradientLayer = CAGradientLayer()

     gradientLayer.colors = [ colorTop, colorBottom]
     gradientLayer.locations = [ 0.5, 0.5]
     gradientLayer.frame = CGRectMake(0, -20, 375, 64)

      self.navigationController?.navigationBar.layer.addSublayer(gradientLayer)

Upvotes: 0

Related Questions