2017-02-16 59 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

不客氣。快樂的編碼。 –