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

时间:2017-02-16 10:32:16

标签: ios swift uinavigationbar

我有FirstViewControllerSecondViewController。它们的UINavigationBar颜色不同。当我显示SecondViewController时,颜色会变细。我用模拟器和慢动画录制了动画。

Animation Show

但是,当我从SecondViewController返回FirstViewController时,颜色不会设置动画,而且所有内容都会立即更改。

Animation Dismiss

这就是我在UINavigationBar中为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
    }
}

在我的FirstViewController类中,我创建了一个结构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,但它也没有改变任何东西。

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

更新

SecondViewController的segue是善意的表现。

我只是用self.performSegue(withIdentifier: "SecondViewControllerSegue", sender: nil)

来称呼它

我没有在后退按钮中添加任何自定义代码,这是默认的UINavigationController实现。

1 个答案:

答案 0 :(得分:2)

尝试使用自定义后退按钮替换后退按钮并向其添加操作。

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