UINavigationController change navigation bar tint color globally and programmatically

55,874

Solution 1

The complete solution which changes the navbar color instantly and remembers the preference for subsequent launches can be found here: iPhone iOS how to redraw UINavigationBar on demand?

Solution 2

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

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

Solution 3

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

Solution 4

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];
Share:
55,874
Alex Stone
Author by

Alex Stone

When people asked me what I wanted to do for work 10 years ago, I did not know too well, so I just said "Something to do with computers". As I look at the last 10 years, I see that I did quite a lot of work all kinds of computers. From fiddling with microcontrollers and assembler code to writing data processing scripts to physically assembling computer consoles. The big step forward came when I learned to think about software in a structured, object-oriented way, as this allowed me to make software do things that I want it to do. Right now I'm proficient in two high level programming languages - Objective-C and Java and have touched just about every framework available for iOS. I've also became a hacker/maker and have completed a number of projects involving software and hardware. Right now I'm in my early 30s and when I ask myself "What would I like to do in the next 10 years?", my answer is "something with the human brain". The seeds are already there - I've picked up an interest in biology, cognitive science and neuroscience, enough to converse with real people. I've done first-hand research into sleep and made discoveries. I've taken classes in synthetic biology, performing manipulations on the bacteria genome. I've learned a lot about the neurotransmitter systems of the human brain, as well as how a biological organism develops. It seems like there are a lot of similarities between the object-oriented concepts I use in the daily programming tasks and how biological organisms operate. This makes me hopeful that by the time I'm in my late 30s, I would be able to work and program some form of biological computer or just plain hack the human brain.

Updated on June 25, 2020

Comments

  • Alex Stone
    Alex Stone about 4 years

    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!

  • thomers
    thomers about 7 years
    Note that UITextAttributeTextColor and UITextAttributeShadowColor have been deprecated - use NSForegroundColorAttributeName and NSShadowAttributeName instead.