Yannick
Yannick

Reputation: 3278

UINavigationBar weird color change animation when dismissing UIViewController

I have a FirstViewController and a SecondViewController. They have different colors for their UINavigationBar. When I show SecondViewController, the color fades in fine. I recorded the animation with the simulator and slow animations.

Animation Show

However, when I go back from SecondViewController to FirstViewController, the color does not animate and everything just changes at once.

Animation Dismiss

This is how I set the code for the UINavigationBar in SecondViewController.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if let navBar = self.navigationController?.navigationBar {

        navBar.barStyle = UIBarStyle.black
        navBar.barTintColor = NavBarColor.red
        navBar.backgroundColor = NavBarColor.red
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        navBar.isTranslucent = false
        navBar.tintColor = .white
    }
}

In my FirstViewController class, I created a struct NavBarSettings and save the information of the UINavigationBar. I then apply them in viewWillAppear.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if let navBar = self.navigationController?.navigationBar,
        let navBarSettings = self.navBarSettings {

        navBar.barStyle = navBarSettings.barStyle
        navBar.barTintColor = navBarSettings.barTintColor
        navBar.backgroundColor = navBarSettings.backgroundColor
        navBar.titleTextAttributes = navBarSettings.titleTextAttributes
        navBar.isTranslucent = navBarSettings.isTranslucent
        navBar.tintColor = navBarSettings.tintColor

    }
}

I also tried to change the UINavigationBar information in SecondViewController viewWillDisappear but it had the same effect.

I've also tried to set a backgroundColor but it had did not change anything either.

How do I get the second animation to work like the first one?

Update

The segue to SecondViewController is of kind show.

I simply call it with self.performSegue(withIdentifier: "SecondViewControllerSegue", sender: nil)

I didn't add any custom code to the back button, it's the default UINavigationController implementation.

Upvotes: 4

Views: 781

Answers (1)

Clinton D'Souza
Clinton D'Souza

Reputation: 301

Try replacing the back button with a custom back button and add an action to it.

let backButton = UIButton()
backButton.addTarget(self, action: #selector(self.backButtonClicked), for: UIControlEvents.touchUpInside)
navBar.navigationItem.leftBarButtonItem = barButton 

func backButtonClicked() {
   // try implementing the same thing here but with the self.navigationController?.popViewController(animated: true)
}

Upvotes: 2

Related Questions