Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController change navigation bar tint color globally and programmatically

This code can change color of a UINavigationBar everywhere within the application. However, I noticed that it does not change the UIColor of the UINavigationBar used by UINavigationController (mine comes from a UIStoryboard).

UIColor* navBarColor = [UIColor colorWithRed:arc4random()%100/100.0 
                                       green:arc4random()%100/100.0 
                                        blue:arc4random()%100/100.0 
                                       alpha:1];
[[UINavigationBar appearance] setTintColor:navBarColor];

[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setAlpha:0.7];

Is there a way to access the appearance object of a UINavigationController's navigation bar? I know how to set tints of individual controllers, but I want to have a global control over how they look.

Update: This was my mistake, the code does change the UIColor of all UINavigationBars, but it requires the root navigation controller to be covered and uncovered(for example presenting a modal view controller), then it will re-draw itself with new UIColors!

Thank you!

like image 677
Alex Stone Avatar asked Feb 23 '12 20:02

Alex Stone


3 Answers

Now in iOS 8 we can set tint color by this :-

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
like image 55
Ashish Avatar answered Nov 12 '22 03:11

Ashish


You can change bar colours globally using appearance proxy:

NSDictionary *textTitleOptions = 
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], 
                                           UITextAttributeTextColor, 
                                           [UIColor whiteColor],  
                                           UITextAttributeTextShadowColor, nil];

[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
textTitleOptions = 
 [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], 
                                            UITextAttributeTextColor, nil];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UIToolbar appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
like image 32
Nilesh Avatar answered Nov 12 '22 03:11

Nilesh


When you declaring your UINavigationController, try this:

UINavigationController *myNavController = 
[[UINavigationController alloc] initWithRootViewController:myRootViewController];
myNavController.navigationBar.tintColor = 
    [UIColor colorWithRed:arc4random() % 100 / 100.0f
                    green:arc4random() % 100 / 100.0f
                     blue:arc4random() % 100 / 100.0f
                    alpha:1.0f];
like image 14
demon9733 Avatar answered Nov 12 '22 02:11

demon9733