Change statusBar background color while using large title for navigation bar on iOS 11

Viewed 7608

I'm trying to use the new navigationBar's large title feature on iOS 11.

However, after I added the following line:

self.navigationController?.navigationBar.prefersLargeTitles = true

I found that the navigationBar background color changed to black.

Navigation Bar 1

So I set background color again manually:

self.navigationController?.setBackgroundColor(UIColor(hexString: 0xFF7E79))

However, I found that the statusBar background color didn't change:

Navigation Bar 2

After I set up the background color of statusBar through this code:

guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return 
statusBar.backgroundColor = UIColor(hexString: 0xFF7E79)

It gives me an ugly 1px black line like this between the statusBar and the navigationBar:

Navigation Bar 3

So what is the correct way to set the background color of navigationBar?

1 Answers

The correct way to set the background color of the UINavigationBar is to use the barTintColor property.

self.navigationController?.navigationBar.barTintColor = .red

You may notice that the color you set can be a little faded. As noted in the documentation:

This color is made translucent by default unless you set the isTranslucent property to false.

See the barTintColor reference on developer.apple.com

Related