UINavigationBar weird color change animation when disabling UIViewController

I have FirstViewControllerand a SecondViewController. They have different colors for their own UINavigationBar. When I show SecondViewController, the color gradually fades. I recorded an animation with a simulator and slow animations.

Animation show

However, when I return from SecondViewControllerto FirstViewController, the color is not animated , and everything changes immediately.

Dismissal animation

This is how I set the code for UINavigationBarin 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 class, FirstViewControllerI created a structure NavBarSettingsand saved information about the UINavigationBar. Then I 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 changing the UINavigationBar's information SecondViewController viewWillDisappear, but it had the same effect.

backgroundColor, .

?

SecondViewController .

self.performSegue(withIdentifier: "SecondViewControllerSegue", sender: nil)

- , UINavigationController.

+4
1

"" "" .

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)
}
+2

Source: https://habr.com/ru/post/1669916/


All Articles