91

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?

Naresh
  • 16,698
  • 6
  • 112
  • 113
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78

12 Answers12

203

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];
ymutlu
  • 6,585
  • 4
  • 35
  • 47
Seb Thiebaud
  • 2,419
  • 1
  • 15
  • 13
  • Thanks, do you know how to remove the white default gradient that comes from the top to make it a solid color? – Jonathan Thurft Jun 09 '13 at 22:23
  • 1
    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. – Seb Thiebaud Jun 09 '13 at 22:24
  • 59
    on iOS 7 that didn't work, I had to use `navigationBar.barTintColor = myColor;` – Juan de la Torre Sep 19 '13 at 17:15
  • 4
    `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. – Sam Dec 15 '14 at 10:19
  • 1
    Swift Version: `UINavigationBar.appearance().barTintColor = myColor` `UINavigationBar.appearance().isTranslucent = false` – heyfrank Jan 15 '18 at 16:58
105

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];
rano
  • 5,616
  • 4
  • 40
  • 66
LJ1
  • 1,051
  • 1
  • 7
  • 2
  • 3
    This should be the accepted answer, works perfectly for iOS7. – joshuahornby10 Mar 07 '14 at 22:39
  • Correct answer for iOS 7. Working for me. – LondonGuy Apr 13 '14 at 15:37
  • The translucency is the requirement to make this work. As the accepted answer doesn't include that, this should be the accepted answer. – Tristan Warner-Smith Oct 27 '14 at 16:43
  • 3
    Just to clarify, the accepted answer is for AppDelegate and this is for within the View Controller. – netwire Nov 05 '14 at 12:25
  • @Dean is right. Accepted answer will take effect for newly created `UIViewControllers`, while this one will show effects whenever is called. – aramusss Apr 30 '15 at 09:27
  • works for iOS 9.3 self.navigationController?.navigationBar.translucent = false self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() self.navigationController?.navigationBar.barTintColor = UIColor(hexString:"#a71428") – HamasN Jun 21 '16 at 13:14
19

For doing this in iOS 7:

[[UINavigationBar appearance] setBarTintColor:myColor];
Lasse Bunk
  • 1,848
  • 20
  • 21
16

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

Peter Kreinz
  • 7,979
  • 1
  • 64
  • 49
Dustin Williams
  • 3,912
  • 2
  • 16
  • 19
10

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

paiv
  • 5,491
  • 22
  • 30
8

Swift:

self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false
Hemang
  • 26,840
  • 19
  • 119
  • 186
6

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!

ronm333
  • 131
  • 1
  • 9
  • Thanks, I found lots of references on how to do this in ObjC and swift, but noone mentioning its available in IB – Nick Jun 26 '15 at 08:27
3

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.

Community
  • 1
  • 1
e1985
  • 6,239
  • 1
  • 24
  • 39
3

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

Naresh
  • 16,698
  • 6
  • 112
  • 113
2

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];
amar
  • 175
  • 3
  • 19
-1

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.

ganka
  • 191
  • 1
  • 11
-1

Dealing with UINavigationController and UINavigationBar in iOS is a hassle. Fortunately, having an open source third-party library can easily solve these problems, hoping to help everyone.

Git repo: NXNavigationExtension

Change UINavigationBar color:

extension YourViewController {
    
    override var nx_titleTextAttributes: [NSAttributedString.Key : Any]? {
        return [NSAttributedString.Key.foregroundColor: .red]
    }
    
}

example

l1Dan
  • 9
  • 6
  • 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? – benhorgen Nov 17 '21 at 14:57