5

Is it possible to set the navigation bar color for just a single View Controller in the navigation hierarchy? Let the default navigation bar color be red and just the last view controller in the line should have a blue one. I've used these two lines to color the navigation bar of said view controller:

navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .white

But when going back (e.g. by pressing the back button) the navigation bar stays blue. Setting the color back to red using above code doesn't do anything.

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
pmax1
  • 226
  • 1
  • 4
  • 17

2 Answers2

3

The navigationBar is shared across all the view controllers that are in the same UINavigationController stack.

If you want to change it's look for a specific view controller, you'll have to set the new style when the view controller is shown and remove it when the view controller is dismissed. This could be done in the viewWillAppear/viewWillDisappear of your view controller for example.

Ludovic Landry
  • 11,606
  • 10
  • 48
  • 80
  • That worked! But now the color transition when going back to the previous view controller is kind of laggy. Is there a workaround to have the color set before the previous view controller appears? – pmax1 Mar 07 '18 at 20:44
  • https://stackoverflow.com/questions/43014716/how-do-i-transition-animate-color-of-uinavigationbar – Ludovic Landry Mar 07 '18 at 20:48
3

I could get the navigation bar to change colors coming from a ViewControllerB to ViewControllerA perfectly fine with your code. I am not sure what your initial problem was. Here is my code which worked:

ViewController A:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = .red
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonAction(_ sender: Any) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "Second")
        //self.present(controller, animated: true, completion: nil)
        self.navigationController?.pushViewController(controller, animated: true)
    }


}

ViewController B:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = .blue
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

It worked without an issue.

userx
  • 1,083
  • 5
  • 18
  • 36