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!
Now in iOS 8 we can set tint color by this :-
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
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]];
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With