ios – 当解雇UIViewController时,UINavigationBar奇怪的颜色更改动画

我有一个FirstViewController和一个SecondViewController.它们的UINavigationBar有不同的颜色.当我显示SecondViewController时,颜色会很好地消失.我用模拟器和慢动画录制了动画.

《ios – 当解雇UIViewController时,UINavigationBar奇怪的颜色更改动画》

但是,当我从SecondViewController返回到FirstViewController时,颜色不会生成动画,一切都会立即更改.

《ios – 当解雇UIViewController时,UINavigationBar奇怪的颜色更改动画》

这是我在SecondViewController中为UINavigationBar设置代码的方法.

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
    }
}

在我的FirstViewController类中,我创建了一个struct NavBarSettings并保存UINavigationBar的信息.然后我在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

    }
}

我还尝试在SecondViewController viewWillDisappear中更改UINavigationBar信息,但它具有相同的效果.

我也试过设置一个backgroundColor,但它也没有改变任何东西.

如何使第二个动画像第一个一样工作?

更新

segue到SecondViewController很实用.

我只是用self.performSegue调用它(withIdentifier:“SecondViewControllerSegue”,sender:nil)

我没有向后退按钮添加任何自定义代码,它是默认的UINavigationController实现.

最佳答案 尝试使用自定义后退按钮替换后退按钮并向其添加操作.

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)
}
点赞