Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 738
David Avatar asked Sep 16 '17 23:09

David


People also ask

How do I change my navigation bar background?

In order to change the hub navigation bar color, we can go to site settings of hub site>Change the look>under the header section>Background> select a theme color to change the background color of your site header.

How do I change the navigation bar on my Iphone?

Change the Bar Style A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I change the background color in navigation bar in Swift?

navigationBar. barTintColor = UIColor. newBlueColor() and of course this just changes the colour of the navigation bar of the view controller that the code is within.

How do I change the navigation bar title color in Swift?

The title color of Navigation Bar can be changed in Storyboard. Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu. Save this answer.


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

like image 130
Moshe Gutman Avatar answered Sep 19 '22 19:09

Moshe Gutman