Search code examples
iosobjective-cuinavigationcontrolleruinavigationbaruinavigationitem

iOS: Changing self.navigationController.navigationBar.tintColor changes for all the navigation bars


I'm navigating to a Navigation controller (Say navC) from a view controller containing a table view (say tableC).

In this navC's rootViewController, I set the backButton's title color in navigation bar using self.navigationController.navigationBar.tintColor = [UIColor redColor].

When I come back to tableC, the back button title color gets changed to redColor.

And it also changes the backButton title color for any other view controller to which I navigate afterwards.


Solution

  • When you push a Viewcontroller it's, pushed over it's Rootviewcontroller. If you change any color (tintColor or barColor), it will also affect the Rootviewcontroller.

    If you need different color for each ViewController, set it in it's viewWillAppear() method.

    If you need to change the bar button color for only one ViewController, set it viewWillAppear and reset it to it's original color in viewWillDisappear

    e.g. Set to Red color

    - (void) viewWillAppear:(BOOL)animated
    {
      self.navigationController.navigationBar.tintColor = [UIColor redColor]  
    }
    

    Reset to White Color

    - (void) viewWillDisappear:(BOOL)animated
    {
      self.navigationController.navigationBar.tintColor = [UIColor whiteColor]  
    }