How to change UINavigationBar background color from the AppDelegate

109,732

Solution 1

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];

Solution 2

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];

Solution 3

For doing this in iOS 7:

[[UINavigationBar appearance] setBarTintColor:myColor];

Solution 4

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

Solution 5

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

Share:
109,732
Jonathan Thurft
Author by

Jonathan Thurft

Updated on July 08, 2022

Comments

  • Jonathan Thurft
    Jonathan Thurft almost 2 years

    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?

  • Jonathan Thurft
    Jonathan Thurft almost 11 years
    Thanks, do you know how to remove the white default gradient that comes from the top to make it a solid color?
  • Seb Thiebaud
    Seb Thiebaud almost 11 years
    With tintColor the gradient will stay. If you don't want to have this gradient, you need to subclass your UINavigationBar OR to make an UIImage for the appearance.
  • Juan de la Torre
    Juan de la Torre over 10 years
    on iOS 7 that didn't work, I had to use navigationBar.barTintColor = myColor;
  • joshuahornby10
    joshuahornby10 about 10 years
    This should be the accepted answer, works perfectly for iOS7.
  • LondonGuy
    LondonGuy about 10 years
    Correct answer for iOS 7. Working for me.
  • Mukesh Nandeda Dhakad
    Mukesh Nandeda Dhakad over 9 years
    The translucency is the requirement to make this work. As the accepted answer doesn't include that, this should be the accepted answer.
  • netwire
    netwire over 9 years
    Just to clarify, the accepted answer is for AppDelegate and this is for within the View Controller.
  • Sam
    Sam over 9 years
    translucent property just can't be set using UIAppearance in iOS 6 and iOS 7. So [[UINavigationBar appearance] setTranslucent:NO] will crash the app with NSInvalidArgumentException error. Use [self.navigationController.navigationBar setTranslucent:NO] or deselect transparency using IB.
  • aramusss
    aramusss about 9 years
    @Dean is right. Accepted answer will take effect for newly created UIViewControllers, while this one will show effects whenever is called.
  • Nick
    Nick almost 9 years
    Thanks, I found lots of references on how to do this in ObjC and swift, but noone mentioning its available in IB
  • HamasN
    HamasN almost 8 years
    works for iOS 9.3 self.navigationController?.navigationBar.translucent = false self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() self.navigationController?.navigationBar.barTintColor = UIColor(hexString:"#a71428")
  • heyfrank
    heyfrank over 6 years
    Swift Version: UINavigationBar.appearance().barTintColor = myColor UINavigationBar.appearance().isTranslucent = false
  • benhorgen
    benhorgen over 2 years
    This appears to be a bit of a advertisement for your library. Can you expand on how the library adds/eases functionality in comparison to the other answers?