2

I have two view controllers in a navigation controller. I'm trying to change the tint color of the navigation bar for each view controller.

I'm using the following code to accomplish this:

FirstVC:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    navigationController?.navigationBar.barTintColor = .yandasRed
    navigationController?.navigationBar.isTranslucent = false
}

SecondVC:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    navigationController?.navigationBar.barTintColor = .white
    navigationController?.navigationBar.isTranslucent = true
}

The transition between FirstVC to SecondVC is seemless, and looks great. However when I go back to the FirstVC from the SecondVC, there is a delay in changing the navigation bar tint color. About 1/2 a second.

Why is this? I was under the impression that all code inside viewWillAppear is executed before the view controller is loaded.

Alex Ritter
  • 1,009
  • 3
  • 18
  • 40
  • Tried the code in the linked question. It crashes the app immediately on loading. I have no understading of how isViewLoaded works, so possibly the code is incomplete? – Alex Ritter Jan 04 '18 at 18:06

1 Answers1

3

Based on the answer here, you can implement:

override func willMove(toParentViewController parent: UIViewController?) {
    super.willMove(toParentViewController: parent)
    // Setup your navigation bar
}

I believe this will eliminate the delay.

Pang
  • 9,564
  • 146
  • 81
  • 122
Nizzam
  • 1,030
  • 3
  • 16
  • 29