How to change UINavigationBar background color from the AppDelegate
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
}
Jonathan Thurft
Updated on July 08, 2022Comments
-
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 theapp delegate
. I do not want to set it each time from each view and I do not want to write aCGRect
.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 almost 11 yearsThanks, do you know how to remove the white default gradient that comes from the top to make it a solid color?
-
Seb Thiebaud almost 11 yearsWith
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 over 10 yearson iOS 7 that didn't work, I had to use
navigationBar.barTintColor = myColor;
-
joshuahornby10 about 10 yearsThis should be the accepted answer, works perfectly for iOS7.
-
LondonGuy about 10 yearsCorrect answer for iOS 7. Working for me.
-
Mukesh Nandeda Dhakad over 9 yearsThe translucency is the requirement to make this work. As the accepted answer doesn't include that, this should be the accepted answer.
-
netwire over 9 yearsJust to clarify, the accepted answer is for AppDelegate and this is for within the View Controller.
-
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 withNSInvalidArgumentException
error. Use[self.navigationController.navigationBar setTranslucent:NO]
or deselect transparency using IB. -
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 almost 9 yearsThanks, I found lots of references on how to do this in ObjC and swift, but noone mentioning its available in IB
-
HamasN almost 8 yearsworks for iOS 9.3 self.navigationController?.navigationBar.translucent = false self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() self.navigationController?.navigationBar.barTintColor = UIColor(hexString:"#a71428")
-
heyfrank over 6 yearsSwift Version:
UINavigationBar.appearance().barTintColor = myColor
UINavigationBar.appearance().isTranslucent = false
-
benhorgen over 2 yearsThis 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?