How to change Navigation Bar color in iOS 7?

293,790

Solution 1

The behavior of tintColor for bars has changed in iOS 7.0. It no longer affects the bar's background.

From the documentation:

barTintColor Class Reference

The tint color to apply to the navigation bar background.

@property(nonatomic, retain) UIColor *barTintColor

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

Availability

Available in iOS 7.0 and later.

Declared In
UINavigationBar.h

Code

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    // iOS 7.0 or later   
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
}else {
    // iOS 6.1 or earlier
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
}

We can also use this to check iOS Version as mention in iOS 7 UI Transition Guide

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        // iOS 6.1 or earlier
        self.navigationController.navigationBar.tintColor = [UIColor redColor];
    } else {
        // iOS 7.0 or later     
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        self.navigationController.navigationBar.translucent = NO;
    }

EDIT Using xib

enter image description here

Solution 2

Doing what the original question asked—to get the old Twitter's Nav Bar look, blue background with white text—is very easy to do just using the Interface Builder in Xcode.

  • Using the Document Outline, select your Navigation Bar.
  • In the Attributes Inspector, in the Navigation Bar group, change the Style from Default to Black. This changes the background colour of the Navigation and Status bars to black, and their text to white. So the battery and other icons and text in the status bar will look white when the app is running.
  • In the same Navigation Bar group, change the Bar Tint to the colour of your liking.
  • If you have Bar Button items in your Navigation Bar, those will still show their text in the default blue colour, so in the Attributes Inspector, View group, change the Tint to White Colour.

That should get you what you want. Here is a screenshot that would make it easier to see where to make the changes.

Where to make the necessary changes, including the result in iOS Simulator

Note that changing only the Bar Tint doesn't change the text colour in the Navigation Bar or the Status Bar. The Style also needs to be changed.

Solution 3

self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;

// *barTintColor* sets the background color
// *tintColor* sets the button's color

Solution 4

In a Navigation based app you can put the code in AppDelegate. A more detailed code could be:

// Navigation bar appearance (background and title)

[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor titleColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"FontNAme" size:titleSize], NSFontAttributeName, nil]];

[[UINavigationBar appearance] setTintColor:[UIColor barColor]];

// Navigation bar buttons appearance

[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor textBarColor], NSForegroundColorAttributeName, shadowColor, NSShadowAttributeName, [UIFont fontWithName:@"FontName" size:titleSize], NSFontAttributeName, nil];

Solution 5

In viewDidLoad, set:

    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];

Change ( blueColor ) to whatever color you'd like.

Share:
293,790

Related videos on Youtube

Patricio Guerra
Author by

Patricio Guerra

15 years old. Wanna do something significant; and I have a hunch I'm getting close.

Updated on May 24, 2021

Comments

  • Patricio Guerra
    Patricio Guerra almost 3 years

    How do I change the Navigation Bar color in iOS 7?

    Basically I want to achieve something like the Twitter Nav Bar (updated Twitter for iOS7 that is). I embedded-in a nav bar atop a view controller. All I want is to change the nav bar color to light blue along with the utility bar at the top. I can't seem to find an option in my storyboard.

  • Patricio Guerra
    Patricio Guerra over 10 years
    And where exactly can I put this code?? Sorry for the newbie question there hahah
  • Patricio Guerra
    Patricio Guerra over 10 years
    but where can I place this code?? Im very new to this hahah. I placed it within the app delegate and it really doesnt seem to be doing much. It builds just fine though.
  • pNre
    pNre over 10 years
    the code can be placed in the - (void)viewDidLoad method of the navigation controller
  • Rajneesh071
    Rajneesh071 over 10 years
    inside viewController
  • Pascal Bayer
    Pascal Bayer over 10 years
    Changing the tintColor property in iOS7 has no longer the effect of changing the bars background color. Tint color in iOS7 only affects the text color of the BarButtonItems etc.
  • HalR
    HalR over 10 years
    I would put it in viewDidLoad in your viewController.
  • David Thompson
    David Thompson over 10 years
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) This would be better to test which iOS version you're running on, as stated by apple in the transition guide: developer.apple.com/library/ios/documentation/userexperience‌​/…
  • IgniteCoders
    IgniteCoders over 10 years
    On iOS 7: -self.navigationController.navigationBar.barTintColor is the bar color AND -self.navigationController.navigationBar.tintColor is the buttons text color (back, done...).
  • SimpsOff
    SimpsOff over 10 years
    You can also use if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) vs the version number to check if you can set the barTintColor
  • Samin
    Samin about 10 years
    add [[UINavigationBar appearance] setBarTintColor:[UIColor barColor]]; for iOS 7
  • Ankish Jain
    Ankish Jain about 10 years
    You should put in viewWillAppear , it wouldnt reflect otherwise.
  • Jayprakash Dubey
    Jayprakash Dubey almost 10 years
    This one helped and +1 for sure!
  • Symmetric
    Symmetric almost 10 years
    Thanks, I had changed this months ago while playing around and couldn't figure out how to change it back!
  • pqsk
    pqsk almost 10 years
    No problem, glad I could help someone
  • lielvilla
    lielvilla over 9 years
    UIStatusBarStyleLightContent works, but it seems the correct enum for setBarStyle: is UIBarStyleBlack
  • Ispas Claudiu
    Ispas Claudiu over 9 years
    I think this is the most straight forward answer! Thx
  • pqsk
    pqsk over 9 years
    No problem. Glad it's still helpful
  • Guillermo Barreiro
    Guillermo Barreiro over 9 years
    Quite easier using the Storyboard!
  • Ken
    Ken over 9 years
    [UINavigationBar appearance] was very helpful.
  • Rob85
    Rob85 over 8 years
    Selecting black for iOS 9 is deprecated, any chance of an updated answer
  • alondono
    alondono over 8 years
    @Rob85 I am not in front of Xcode right now, but when Xcode 7 (with iOS 9) was released I verified that these instructions were still up-to-date. Make sure that the Navigation Bar is selected, the black style should be in the attributes inspector of the Navigation Bar.
  • alondono
    alondono over 8 years
    @Rob85 I've checked Apple's Developer website, and styles Default and Black are still available in iOS 9. The deprecated ones are BlackOpaque and BlackTranslucent, which were replaced by a combination of Black style and the Translucent tick-box. UIBarStyle on Apple's Developer website
  • Rob85
    Rob85 over 8 years
    Sorry @alondono you are absolutely correct, i was clicking on the navigation bar on the Navigation Controller not in document outline thats why i couldn't see it . The black that i am seeing deprecated was on the simulated metrics->status bar. Thanks for your replies
  • alondono
    alondono over 8 years
    @Rob85 Cool! Thanks for letting me know.
  • Anton Tropashko
    Anton Tropashko about 8 years
    any ideas why apple needed an extra prop when there is backgroundColor already?
  • TheJeff
    TheJeff over 7 years
    In iOS 9, setting the barTintColor to black will make the time, device name and battery black too so you can't see them. To avoid this, instead of using barTintColor, use self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  • John Doe
    John Doe over 7 years
    This answer still works. I like configuring the visuals using visual editor, and leave the code to do actual logic.