setTintColor: on UINavigationBar appearance has no effect

7k Views Asked by At

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];
2

There are 2 best solutions below

0
On BEST ANSWER

From lots more digging, I have learned that setting the tintColor property to a color made from an image is considered a 'hack'. Only RGB colors are supported and expected to work properly for this property. So there you have it. Because of the NDA I can't give any more details, but once iOS 6 is public, I'll try to update this.

0
On

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;