Search code examples
swiftuinavigationbaruistatusbar

Status bar and navigation bar does not seems to have the same color iOS Swift


I want to set the status bar color the same color as the navigation bar. when I try to set the navigation and status bar to the same color the navigation bar always appear in a lighter color than the status bar.

This is what I want:

enter image description here

My result:

enter image description here

Code in AppDelegate:

Status bar:

UIApplication.sharedApplication().statusBarStyle = .Default
    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector(Selector("setBackgroundColor:")) 
{
      statusBar.backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
      statusBar.tintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
}

Navigation bar:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        UINavigationBar.appearance().backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UIApplication.sharedApplication().statusBarHidden = false
        UIApplication.sharedApplication().statusBarStyle = .Default

Could anyone give me any suggestion on how to solve this problem.

Thanks in advance!


Solution

  • To achieve the desired result, set the status bar style to default and set the UINavigationBar.appearance().barTintColor to the required color.

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UINavigationBar.appearance().barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
    UIApplication.sharedApplication().statusBarStyle = .Default