2017-02-16 62 views
4

我有FirstViewControllerSecondViewController。他们有UINavigationBar不同的颜色。当我显示SecondViewController时,颜色淡化。我用模拟器和慢动画录制了动画。UINavigationBar奇怪的颜色更改动画时解雇UIViewController

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 

    } 
} 

我也试图改变UINavigationBar的信息SecondViewControllerviewWillDisappear但它有同样的效果。

我也尝试设置一个backgroundColor但它并没有改变任何东西。

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

更新

的原因请看SecondViewController是实物展示。

我只是把它与self.performSegue(withIdentifier: "SecondViewControllerSegue", sender: nil)

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

+0

对不起,我误解了你的问题,我删除了答案。您是否尝试过只是手动设置值,而不是仅仅在您的FirstViewController的viewWillAppear方法中从navBarSettings中设置值? – 2017-02-16 10:43:13

+0

是的,我做了,但没有区别 – Yannick

+0

你可以用代码来更新你的问题,你如何推送SecondViewController以及如何弹出它? – 2017-02-16 10:52:56

回答

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

谢谢!:)'self.navigationController?.popViewController(animated:true)'部分诀窍 – Yannick

+2

不客气。快乐的编码。 –