我有2个视图控制器,连接视图show(push)segue。在初始视图控制器中,我有以下内容来控制导航栏的样式:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "ProximaNova-Semibold", size: 18)!, NSForegroundColorAttributeName: UIColorFromRGB("292929")]
self.navigationController?.navigationBar.barTintColor = UIColorFromRGB("f7f7f7")
self.navigationController?.navigationBar.layer.borderColor = UIColorFromRGB("E9E9E9").CGColor
}
在目标视图控制器中,我有以下内容来控制该控制器中导航栏的样式:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "ProximaNova-Semibold", size: 18)!, NSForegroundColorAttributeName: UIColorFromRGB("FFFFFF")]
}
问题是当我单击目标视图控制器上的后退按钮以返回到初始视图控制器时,导航栏将设置为目标视图控制器的样式。有解决方案吗
答案 0 :(得分:1)
您应该在viewWillAppear方法中为两个控制器编写导航栏样式的代码,如下所示。 因为viewDidLoad方法在每个视图生命周期中被调用一次。一旦加载了视图,这个方法就不会被再次执行。因此,我们必须使用 viewWillAppear
而不是viewDidLoad初始视图控制器代码
override func viewWillAppear() {
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "ProximaNova-Semibold", size: 18)!, NSForegroundColorAttributeName: UIColorFromRGB("292929")]
self.navigationController?.navigationBar.barTintColor = UIColorFromRGB("f7f7f7")
self.navigationController?.navigationBar.layer.borderColor = UIColorFromRGB("E9E9E9").CGColor
}
目标视图控制器代码
override func viewWillAppear() {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "ProximaNova-Semibold", size: 18)!, NSForegroundColorAttributeName: UIColorFromRGB("FFFFFF")]
}