我有FirstViewController
和SecondViewController
。它们的UINavigationBar
颜色不同。当我显示SecondViewController
时,颜色会变细。我用模拟器和慢动画录制了动画。
但是,当我从SecondViewController
返回FirstViewController
时,颜色不会设置动画,而且所有内容都会立即更改。
这就是我在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
实现。
答案 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)
}