Set tintColor for UINavigationBar only for specific child view controller

4.6k views Asked by At

I have a basic master-detail app. Depending on the item that is selected in the master list, I'd like to set the tintColor of the navigation bar in the details view only.

I set the tint color like so: self.navigationController.navigationBar.tintColor = detailTintColor; in viewWillAppear: of the details controller.

However, this also changes the color of the master controller's navigation bar to yellow (should remain red), which is visible as a flickering during the transition animation. I assume because there is only one navigation bar instance.

Here a screenshot to illustrate the problem:

  • The "i" icon is in the master view controller's navbar and should be red, not yellow

enter image description here

3

There are 3 answers

0
Ashish On BEST ANSWER

In iOS 8 we set tint color by this :-

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
1
thorb65 On

why not resetting to original color when view disapperars?

-(void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.tintColor = yourSavedColor;
    [super viewWillDisappear:animated];
}
5
redent84 On

You only have to change the tint color of the UINavigationBar in the Details UINavigationController in Storyboard:

enter image description here

Working sample:

enter image description here

You could use the UIAppearance API to obtain the same result by subclassing UINavigationController, and setting the details controller to be that custom class. Then, using UIAppearance API, set the UINavigationBar tint color:

[[UINavigationBar appearanceWhenContainedIn:[DetailNavigationController class], nil] setTintColor:[UIColor redColor]];