64

I want to change the color of the navigation bar color, but I'm not sure whether or not I should change the tint or the background. I know iOS 7 is going for a more flat design (even recommending removing gradients), but I am having trouble deciphering the two. Even if I set a background color, it doesn't do anything.

In this image, the background is set to green, but the bar is still blue:

Enter image description here

1

16 Answers 16

107

The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background and behaves as described for the tintColor property added to UIView. To tint the bar's background, please use -barTintColor.

navController.navigationBar.barTintColor = [UIColor navigationColor];

3
79

If you want to have a solid color for your navigation bar in iOS 6 similar to iOS 7 use this:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor greenColor]];

in iOS 7 use the barTintColor like this:

navigationController.navigationBar.barTintColor = [UIColor greenColor];

or

 [[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
2
  • I am using a custom navigation bar and the above code was not working for me (ios7). I had to explicitly write this: [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; Apr 30, 2014 at 13:22
  • [[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]]; should work in iOS 7
    – Carmen
    May 6, 2014 at 11:44
36

// In ios 7 :-

[self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];

// In ios 6 :-

[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
1
  • Thanks its working but how to change purticular viewcontroller navigation bar color please help me
    – vijay
    Jan 20, 2015 at 13:53
11

The background color property is ignored on a UINavigationBar, so if you want to adjust the look and feel you either have to use the tintColor or call some of the other methods listed under "Customizing the Bar Appearance" of the UINavigationBar class reference (like setBackgroundImage:forBarMetrics:).

Be aware that the tintColor property works differently in iOS 7, so if you want a consistent look between iOS 7 and prior version using a background image might be your best bet. It's also worth mentioning that you can't configure the background image in the Storyboard, you'll have to create an IBOutlet to your UINavigationBar and change it in viewDidLoad or some other appropriate place.

5

One more thing, if you want to change the navigation bg color in UIPopover you need to set barStyle to UIBarStyleBlack

if([UINavigationBar instancesRespondToSelector:@selector(barTintColor)]){ //iOS7
    navigationController.navigationBar.barStyle = UIBarStyleBlack;
    navigationController.navigationBar.barTintColor = [UIColor redColor];
}
4

Here is how to set it correctly for both iOS 6 and 7.

+ (void)fixNavBarColor:(UINavigationBar*)bar {
    if (iosVersion >= 7) {
        bar.barTintColor = [UIColor redColor];
        bar.translucent = NO;
    }else {
        bar.tintColor = [UIColor redColor];
        bar.opaque = YES;
    }
}
2
  • 3
    Instead of checking for iosVersion you should use respondsToSelector i.e. [[UINavigationBar appearance] respondsToSelector:@selector(barTintColor)]
    – smitt04
    Nov 13, 2013 at 0:02
  • 2
    For proxies you should use instancesRespondToSelector. For this case it would be [UINavigationBar instancesRespondToSelector:@selector(barTintColor)] Nov 14, 2013 at 20:26
4

The complete code with version checking.

 if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer
    [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
}
else {

    // do stuff for older versions than iOS 7
    [self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
}
4

You can check iOS Version and simply set the tint color of Navigation bar.

if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9529 green:0.4392 blue:0.3333 alpha:1.0];
}else{

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9529 green:0.4392 blue:0.3333 alpha:1.0];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
3

Based on posted answered, this worked for me:

/* check for iOS 6 or 7 */
if ([[self navigationController].navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
    [[self navigationController].navigationBar setBarTintColor:[UIColor whiteColor]];

} else {
    /* Set background and foreground */
    [[self navigationController].navigationBar setTintColor:[UIColor whiteColor]];
    [self navigationController].navigationBar.titleTextAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,nil];
}
2
    you can add bellow code in appdelegate.m .if your app is navigation based

    // for background color
   [nav.navigationBar setBarTintColor:[UIColor blueColor]];

    // for change navigation title and button color
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],
    NSForegroundColorAttributeName,               
    [UIFont fontWithName:@"FontNAme" size:20],
    NSFontAttributeName, nil]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
2

Insert the below code in didFinishLaunchingWithOptions() in AppDelegate.m

[[UINavigationBar appearance] setBarTintColor:[UIColor
    colorWithRed:26.0/255.0 green:184.0/255.0 blue:110.0/255.0 alpha:1.0]];
1

I'm using following code (in C#) to change the color of the NavigationBar:

NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.LandscapePhone);
NavigationController.NavigationBar.BackgroundColor = UIColor.Green;

The trick is that you need to get rid of the default background image and then the color will appear.

2
  • Your code works for the navigationbar, but are you able to apply the same background to the status bar?
    – axelcdv
    Sep 3, 2013 at 20:51
  • As far as I know in iOS6 you cannot change the status bar color. You only can make it "translucent". In iOS7 you can change the color of status bar but it is achieved through background image of NavigationBar. In iOS7 I'm using 1x1 pixel image with preferred color as NavigationBar background image and it is automatically extended also to the status bar (as described in Apple official iOS7 documentation).
    – tomec
    Sep 3, 2013 at 22:52
1

If you want to change a color of a navigation bar, use barTintColor property of it. In addition, if you set any color to tintColor of it, that affects to the navigation bar's item like a button.

FYI, you want to keep iOS 6 style bar, make a background image looks like previous style and set it.

For more detail, you can get more information from the following link:

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/AppearanceCustomization.html

1

In iOS7, if your navigation controller is contained in tab bar, splitview or some other container, then for globally changing navigationbar appearance use following method ::

[[UINavigationBar appearanceWhenContainedIn:[UITabBarController class],nil] setBarTintColor:[UIColor blueColor]];
0

Try the code below in the - (void)viewDidLoad of your ViewController.m

[[[self navigationController] navigationBar] setTintColor:[UIColor yellowColor]];

this did work for me in iOS 6.. Try it..

-4

I'm not sure about changing the tint vs the background color but this is how you change the tint color of the Navigation Bar:

Try this code..

[navigationController.navigationBar setTintColor:[UIColor redColor]; //Red as an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.