Jonathan Thurft
Jonathan Thurft

Reputation: 4173

How to change UINavigationBar background color from the AppDelegate

I know how to change the UINavigationBar background image by doing

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nabbar"] forBarMetrics:UIBarMetricsDefault];

and I know how to set the bar to different colors within each Views..... Now I want to change the background color without using an image to a solid color from the app delegate. I do not want to set it each time from each view and I do not want to write a CGRect.

I tried [[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]]; but I doesn't work and I cant find a code anywhere that works in the app delegate.

Could anyone please point me in the right direction?

Upvotes: 92

Views: 115388

Answers (11)

paiv
paiv

Reputation: 5601

iOS 13.0 introduced new API for this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let myColor = UIColor(hue: 0.4, saturation: 0.25, brightness: 1, alpha: 1)
    
    let barAppearance = UINavigationBarAppearance()
    barAppearance.backgroundColor = myColor

    let navigationBar = UINavigationBar.appearance()
    navigationBar.standardAppearance = barAppearance
    navigationBar.scrollEdgeAppearance = barAppearance // for scrollable content or large titles
    
    return true
}

navigation bar

Upvotes: 14

Naresh
Naresh

Reputation: 17902

In Swift 4.2 and Xcode 10.1

You can change your navigation bar colour from your AppDelegate directly to your entire project.

In didFinishLaunchingWithOptions launchOptions: write below to lines of code

UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)

Here

tintColor is for to set background images like back button & menu lines images etc. (See below left and right menu image)

barTintColor is for navigation bar background colour

If you want to set specific view controller navigation bar colour, write below code in viewDidLoad()

//Add navigation bar colour
navigationController?.navigationBar.barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
navigationController?.navigationBar.tintColor = UIColor.white

enter image description here

Upvotes: 3

Hemang
Hemang

Reputation: 27050

Swift:

self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false

Upvotes: 8

ganka
ganka

Reputation: 309

The colour code is the issue here. Instead of using 195/255, use 0.7647 or 195.f/255.f The problem is converting the float is not working properly. Try using exact float value.

Upvotes: -1

Seb Thiebaud
Seb Thiebaud

Reputation: 2419

You can use [[UINavigationBar appearance] setTintColor:myColor];

Since iOS 7 you need to set [[UINavigationBar appearance] setBarTintColor:myColor]; and also [[UINavigationBar appearance] setTranslucent:NO].

[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];

Upvotes: 203

amar
amar

Reputation: 185

You can set UINavigation Background color by using this code in any view controller

self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];

Upvotes: 2

ronm333
ronm333

Reputation: 131

You can easily do this with Xcode 6.3.1. Select your NavigationBar in the Document outline. Select the Attributes Inspector. Uncheck Translucent. Set Bar Tint to your desired color. Done!

Upvotes: 6

Dustin Williams
Dustin Williams

Reputation: 3913

Swift syntax:

    UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color

I just put that in the AppDelegate didFinishLaunchingWithOptions and it persists throughout the app

Upvotes: 16

Lasse Bunk
Lasse Bunk

Reputation: 1858

For doing this in iOS 7:

[[UINavigationBar appearance] setBarTintColor:myColor];

Upvotes: 19

LJ1
LJ1

Reputation: 1051

To change the background color and not the tint the following piece of code will work:

[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.navigationController.navigationBar setTranslucent:NO];

Upvotes: 105

e1985
e1985

Reputation: 6279

As the other answers mention, you can use setTintColor:, but you want a solid color and that's not possible to do setting the tint color AFAIK.

The solution is to create an image programmatically and set that image as the background image for all navigation bars via UIAppearance. About the size of the image, I'm not sure if a 1x1 pixel image would work or if you need the exact size of the navigation bar.Check the second answer of this question to see how to create the image.

As an advice, I don't like to "overload" the app delegate with these type of things. What I tend to do is to create a class named AppearanceConfiguration with only one public method configureAppearance where I set all the UIAppearance stuff I want, and then I call that method from the app delegate.

Upvotes: 3

Related Questions