Daryl Wong
Daryl Wong

Reputation: 2443

Setting status bar and navigation bar to be the same color

enter image description here

Hi, I wanted to set the status bar to have the exact same color as the UINavigationBar. I am using the exact same color in the code shown below but as you can see from the image, they are not of the same blue color. Is there any method to get them to be the same. I tried to google but could not find any solution.

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)

UINavigationBar.appearance().barTintColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)

Upvotes: 0

Views: 6373

Answers (5)

pyromancer2
pyromancer2

Reputation: 173

You should not have to set the status bar color if you want it to match the navbar. Commenting out the following line solves this issue in my environment.

Screenshot of the results

// UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)

UINavigationBar.appearance().barTintColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)

Alternatively, you could do the following:

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 0.0)

UINavigationBar.appearance().barTintColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 0.0)

Upvotes: 0

Shezad
Shezad

Reputation: 756

try changing translucent to false

navigationBar.isTranslucent = false

Upvotes: 3

Kaushik Makwana
Kaushik Makwana

Reputation: 1327

You can add below function to AppDelegate.swift file

func changeStatusBarColor(_ color: UIColor) {
        guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
            return
        }
        statusBar.backgroundColor = color
    }

and Use in didFinishLaunchingWithOptions method like below

self.changeStatusBarColor(UIColor.black) //pass the color you want to set

Edited

You can Use shared instance in AppDelegate.swift file

static let shared = AppDelegate()

After that whenever you set navigation background color just pass it to changeStatusBarColor method

Use below method to set same as navigation background color

AppDelegate.shared.changeStatusBarColor((self.navigationController?.navigationBar.backgroundColor)!)

Upvotes: 0

Sagar Dhake
Sagar Dhake

Reputation: 1

self.navigationController.navigationBar.barTintColor = UIColor.yourColor

Upvotes: 0

Quyen Anh Nguyen
Quyen Anh Nguyen

Reputation: 1790

 self.navigationController?.navigationBar.tintColor = UIColor.blue 
 self.navigationController?.navigationBar.barTintColor = UIColor.blue

I run on xcode 10.1, swift 4.2. Hope this helps.

Upvotes: 0

Related Questions