235

I want to set background for Navigation Bar to be black and all colors inside it to be white.

So, I used this code :

[[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor whiteColor],
      NSForegroundColorAttributeName,
      [UIColor whiteColor],
      NSForegroundColorAttributeName,
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
      NSForegroundColorAttributeName,
      [UIFont fontWithName:@"Arial-Bold" size:0.0],
      NSFontAttributeName,
      nil]];

But back button text color, arrow and bar button have still default blue color.
How to change those colors like on image below?

navigation bar

shim
  • 9,289
  • 12
  • 69
  • 108
1110
  • 7,829
  • 55
  • 176
  • 334
  • 1
    try in AppDelegate if ([self.window respondsToSelector:@selector(setTintColor:)]) self.window.tintColor = [UIColor whiteColor]; – Evgeniy S Sep 26 '13 at 13:41
  • 3
    http://stackoverflow.com/questions/18384488/ios-7-uibarbutton-back-button-arrow-color – Gajendra K Chauhan Jan 04 '14 at 03:45
  • Interestingly your code worked great for me (just wanted to change the text color to white). Thanks! – William T. Mallard Sep 14 '14 at 19:03
  • What is the blue color you are using? Looks nice! – ebi May 20 '15 at 19:14
  • @ebi some people don't like sharing. Just user a colour picker to pick it out. Or....there you go: RGB:(18, 60, 133) – pnizzle Jul 17 '15 at 07:06
  • Thanks, but the color on your screen is not the same as what is actually used in the code or displayed on a device. For example, my screen shows RGB(20, 71, 140) using the same image. – ebi Jul 17 '15 at 15:01

11 Answers11

754

Behavior from some of the properties of UINavigationBar has changed from iOS 7. You can see in the image shown below :

enter image description here


Two beautiful links I'd like to share with you. For more details you can go through these links :

  1. iOS 7 UI Transition Guide.
  2. How to Update Your App for iOS 7.

Apple Documentation for barTintColor says :

This color is made translucent by default unless you set the translucent property to NO.

Sample Code :

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar 
 setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.translucent = NO;
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • I have tried: self.nav.navigationBar.tintColor = [UIColor whiteColor]; self.nav.navigationBar.barTintColor = [UIColor colorWithRed:6.0/255.0 green:12.0/255.0 blue:19.0/255.0 alpha:1.0]; But everything is still blue. – 1110 Sep 26 '13 at 13:50
  • 2
    I read it before but this change only text but not and arrow. It's still blue :( – 1110 Sep 26 '13 at 14:04
  • 1
    @1110 : Take a look at : [Using Tint Color](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/AppearanceCustomization.html#//apple_ref/doc/uid/TP40013174-CH15-SW3) – Bhavin Sep 26 '13 at 14:18
  • Thanks, this is great! One question: is it possible to set the size (of the text and arrow) of the back button? The default is obnoxiously big next to the bar title. – JohnK Oct 10 '13 at 19:32
  • How to change font of titleTextAttribute – Suraj K Thomas Jun 23 '14 at 15:11
  • 2
    @Chelsea: How about this line? : `[self.navigationController.navigationBar setTitleTextAttributes:@{[UIFont fontWithName:@"YOURFONT" size:14], NSFontAttributeName}];` – Bhavin Jun 24 '14 at 05:38
  • Thanks, and also by UIAppearance Protocol using `[[UINavigationBar appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];` – Des Sep 10 '14 at 11:43
  • 4
    Don't forget you can change the tint for the entire app in the appDelegate: [[UINavigationBar appearance] setTintColor:[UIColor blackColor]]; – sam_smith Jul 08 '15 at 11:27
86

enter image description here

This one took me about half a day to figure out but this is what worked for me. Inside the rootViewController that initializes the navigationController, I put this code inside my viewDidAppear method:

//set bar color
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:85.0/255.0 green:143.0/255.0 blue:220.0/255.0 alpha:1.0]];
//optional, i don't want my bar to be translucent
[self.navigationController.navigationBar setTranslucent:NO];
//set title and title color
[self.navigationItem setTitle:@"Title"];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
//set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//set back button arrow color
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

My full post on this here

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • 1
    If you are having problems changing the color of the back arrow, this snippet will fix the problem. At a minimum, you'll need the last line of code added to your actual rootViewController. – russes Nov 24 '13 at 23:57
  • Yeah, John. I wasted about an hour on this yesterday as well. One problem (big problem) was that the docs were terrible in pointing out all the possible attributes. Any idea where they might be listed? TY. – Alex Zavatone Dec 19 '13 at 15:24
  • Thanks @john It's overlapping my Custom Colour,Can you explain me why? – Vaibhav Limbani Apr 03 '14 at 09:31
  • I think you should create a new question. I couldn't tell you why unless I see your code and what you are doing. @MacGeek – John Riselvato Apr 03 '14 at 15:12
  • 3
    Thanks, it worked :). But UITextAttributeTextColor is deprecated in iOS 7, now you should use NSForegroundColorAttributeName – YYamil Apr 19 '15 at 16:03
  • I was doing all this in `viewDidLoad` and it wasn't working...this did the trick. Still not sure why it doesn't work in `viewDidLoad`... – mfaani May 16 '17 at 00:33
38

Swift3, ios 10

To globally assign the color, in AppDelegate didFinishLaunchingWithOptions:

let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes

Swift 4, iOS 11

let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Adrian
  • 16,233
  • 18
  • 112
  • 180
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
21

Vin's answer worked great for me. Here is the same solution for C# developers using Xamarin.iOS/MonoTouch:

var navigationBar = NavigationController.NavigationBar; //or another reference
navigationBar.BarTintColor = UIColor.Blue;
navigationBar.TintColor = UIColor.White;
navigationBar.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.White });
navigationBar.Translucent = false;
benhorgen
  • 1,928
  • 1
  • 33
  • 38
12

Swift / iOS8

let textAttributes = NSMutableDictionary(capacity:1)
textAttributes.setObject(UIColor.whiteColor(), forKey: NSForegroundColorAttributeName)
navigationController?.navigationBar.titleTextAttributes = textAttributes
RameshVel
  • 64,778
  • 30
  • 169
  • 213
LondonGuy
  • 10,778
  • 11
  • 79
  • 151
  • 3
    let textAttributes = NSMutableDictionary(capacity:1) is now the correct call for the NSMutableDictionary initialization. – juliensaad Nov 30 '14 at 19:09
11
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Sebyddd
  • 4,305
  • 2
  • 39
  • 43
Atef
  • 2,872
  • 1
  • 36
  • 32
  • 1
    I like this approach as you can set this in AppDelegate itself, and it uses the Appearance proxy. –  Apr 30 '15 at 10:42
8

To change color of UINavigationBar title the correct way use this code:

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];

UITextAttributeTextColor is deprecated in lastest ios 7 version. Use NSForegroundColorAttributeName instead.

Ravi Gautam
  • 960
  • 2
  • 9
  • 20
aZtraL-EnForceR
  • 1,781
  • 2
  • 16
  • 19
5

If you're looking to change the title text size and the text color you have to change the NSDictionary titleTextAttributes, for 2 of its objects:

    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13.0],NSFontAttributeName,
                                                                  [UIColor whiteColor], NSForegroundColorAttributeName, 
                                                                  nil];
OhadM
  • 4,687
  • 1
  • 47
  • 57
2

I think previous answers are correct , this is another way of doing the same thing. I am sharing it here with others just in case if it becomes useful for someone. This is how you can change the text/title color for the navbar in ios7:

UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];
NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];
self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes;
grepit
  • 21,260
  • 6
  • 105
  • 81
1

It seems that Accessibility controls in the iOS Settings override pretty much everything you try to do color-wise to the navigation bar buttons. Make sure you have all the settings to the default positions (set increase contrast, bold text, button shapes, etc to off) otherwise you won't see anything change. Once I did it, all the color change code started working as expected. You might not need to turn them all off, but I didn't pursue it further.

Patrick
  • 1,629
  • 5
  • 23
  • 44
strangeluck
  • 846
  • 9
  • 10
  • Exactly what happened to me. I was unable to change the tint color of a bar button item when "Bold Text" was on. That is not good. Now I will have to use a graphic instead :-( – Ken Roy Oct 07 '17 at 14:51
0

Swift 5/iOS 13

To change color of title in controller:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52