ViewWillAppear 中的代码不起作用,为什么?
Code in ViewWillAppear doesn't work, why?
我的应用程序以标签栏控制器开始,然后每个标签都带有导航 VC。我希望每当我开始导航到第二个 VC 时隐藏底部的标签栏,并将导航栏的颜色更改为橙色,这是第二个 VC 中的代码:
override func viewWillAppear(animated: Bool) {
var tabBarHide = self.tabBarController!.tabBar.hidden
print(tabBarHide)
if !tabBarHide {
tabBarHide = true
}
print(tabBarHide)
UINavigationBar.appearance().barTintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)
}
它确实打印出:false & true 每次我导航到这个 VC,但视图没有任何反应。它不起作用。为什么?
您不能使用 UIAppearance
代理更改视图层次结构中已有对象的外观。来自 UIAppearance
documentation:
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
您可以直接修改活动导航栏的色调:
self.navigationController?.navigationBar.tintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)
至于为什么你的标签栏没有隐藏,你修改的是你的局部变量,而不是标签栏的hidden
属性。你想要:
self.tabBarController?.tabBar.hidden = true
所以你的 viewWillAppear
应该是这样的:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true
self.navigationController?.navigationBar.tintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)
}
我的应用程序以标签栏控制器开始,然后每个标签都带有导航 VC。我希望每当我开始导航到第二个 VC 时隐藏底部的标签栏,并将导航栏的颜色更改为橙色,这是第二个 VC 中的代码:
override func viewWillAppear(animated: Bool) {
var tabBarHide = self.tabBarController!.tabBar.hidden
print(tabBarHide)
if !tabBarHide {
tabBarHide = true
}
print(tabBarHide)
UINavigationBar.appearance().barTintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)
}
它确实打印出:false & true 每次我导航到这个 VC,但视图没有任何反应。它不起作用。为什么?
您不能使用 UIAppearance
代理更改视图层次结构中已有对象的外观。来自 UIAppearance
documentation:
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
您可以直接修改活动导航栏的色调:
self.navigationController?.navigationBar.tintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)
至于为什么你的标签栏没有隐藏,你修改的是你的局部变量,而不是标签栏的hidden
属性。你想要:
self.tabBarController?.tabBar.hidden = true
所以你的 viewWillAppear
应该是这样的:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true
self.navigationController?.navigationBar.tintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)
}