setTintColor: on UINavigationBar appearance has no effect

eric.mitchell picture eric.mitchell · Jan 25, 2012 · Viewed 7k times · Source

This is my code to change the color of my app's tab bar and navigation bar:

UIColor* color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue.jpeg"]];

//set colors
[[UINavigationBar appearance] setTintColor:color];
[[UITabBar appearance] setTintColor:color];

Yet only the tab bar tint color changes; the navigation bar stays black. Why does setTintColor: work for the tab bar but not the navigation bar?

Edit: Interestingly enough, when testing on a real device (an iPhone 4 running iOS 5.0.1) neither the tab bar nor the navigation bar's color is changed; both stay black. On the simulator at least the tab bar changes... any explanation for this behavior? Thanks!

Edit 2: Here's part of my code in applicationDidFinishLaunching:

UIColor* color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue.jpeg"]];

//create navigation controllers
firstNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController1];
[[firstNavigationController navigationBar] setTintColor:color];
//[[firstNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

secondNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController2];
[[secondNavigationController navigationBar] setTintColor:color];
//[[secondNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

thirdNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController3];
[[thirdNavigationController navigationBar] setTintColor:color];
//[[thirdNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

fourthNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController4];
[[fourthNavigationController navigationBar] setTintColor:color];
//[[fourthNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

//create tab bar controller
self.tabBarController = [[UITabBarController new] autorelease];
self.tabBarController.delegate = self;

//set controllers
self.tabBarController.viewControllers = [NSArray 
    arrayWithObjects:firstNavigationController, secondNavigationController, 
    thirdNavigationController, fourthNavigationController, nil];

Answer

alloc_iNit picture alloc_iNit · Jan 25, 2012

Find RGB from your "blue.jpeg" and set them as tint color...

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:213.0/255.0 green:166.0/255.0 blue:39.0/255.0 alpha:1];

Or as you are setting an image as background color, you can go with below code...

self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"blue.jpeg"].CGImage;