Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set tintColor for UINavigationBar only for specific child view controller

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

like image 462
Mark Avatar asked Dec 10 '13 11:12

Mark


3 Answers

In iOS 8 we set tint color by this :-

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
like image 200
Ashish Avatar answered Sep 28 '22 10:09

Ashish


why not resetting to original color when view disapperars?

-(void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.tintColor = yourSavedColor;
    [super viewWillDisappear:animated];
}
like image 29
thorb65 Avatar answered Sep 28 '22 11:09

thorb65


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]];
like image 38
redent84 Avatar answered Sep 28 '22 11:09

redent84