Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - UINavigationController bar color does not change programmatically

I have a problem with changing the background color of my UINavigationBar. Here's what I tried so far:

    self.navigationController?.navigationBar.translucent = true
    self.navigationController?.navigationBar.backgroundColor = UIColor.blueColor()

The navigation bar does not change. Even setting the translucency to true has no effect at all. I can change the tint color without any problems, but not the background color.

But if I change the color in the storyboard, it works fine!

Is there an option in the storyboard that I enabled/disabled by mistake? Can someone help me with this? Thanks.

like image 548
Mike_NotGuilty Avatar asked Oct 30 '14 09:10

Mike_NotGuilty


People also ask

How do I change the navigation color in swift 5?

navigationController. 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 color of my navigation bar in CSS?

Here's a way of changing the color. You can also use images or css to use gradients. #nav { position: relative; left: -90px; background-color: #336699; // change #000088 (blue) for the color your want color: #FFFFFF; // change #FFFFFF (white) for the color you want. }

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


2 Answers

You should set the navigation bar's barTintColor instead of backgroundColor. This should be what you are looking for.

like image 54
JoeFryer Avatar answered Oct 20 '22 00:10

JoeFryer


self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.backgroundColor = UIColor.blue

This will solve it.

like image 29
Vysakh P Nair Avatar answered Oct 20 '22 00:10

Vysakh P Nair