ViewWillAppear中的代码不起作用,为什么?

时间:2022-02-21 07:21:47

my app start with a tab bar controller, then every tab goes with a navigation VC. I wish to hide tab bar in the bottom whenever I start to navigate into a second VC, and also change the color of navigation bar to orange color, here's the code in the 2nd VC:

我的应用程序以标签栏控制器开始,然后每个标签都带有导航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)

}

It does print out: false & true every time I navigate to this VC, but nothing happened to the view. It doesn't work. Why?

每次我导航到这个VC时,它都打印出:false&true,但视图没有任何反应。它不起作用。为什么?

1 个解决方案

#1


1  

You can't use the UIAppearance proxy to alter the appearance of an object that is already in the view hierarchy. From the UIAppearance documentation:

您不能使用UIAppearance代理来更改已在视图层次结构中的对象的外观。从UIAppearance文档:

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.

当视图进入窗口时,iOS会应用外观更改,它不会更改已在窗口中的视图的外观。要更改当前在窗口中的视图的外观,请从视图层次结构中删除该视图,然后将其放回。

You can modify the tint color of the active navigation bar directly:

您可以直接修改活动导航栏的色调颜色:

self.navigationController?.navigationBar.tintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)

As for why your tab bar isn't hiding, you are modifying your local variable, not the hidden property of the tab bar. You want:

至于您的标签栏没有隐藏的原因,您正在修改本地变量,而不是标签栏的隐藏属性。你要:

self.tabBarController?.tabBar.hidden = true

So your viewWillAppear should be something like:

所以你的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)

}

#1


1  

You can't use the UIAppearance proxy to alter the appearance of an object that is already in the view hierarchy. From the UIAppearance documentation:

您不能使用UIAppearance代理来更改已在视图层次结构中的对象的外观。从UIAppearance文档:

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.

当视图进入窗口时,iOS会应用外观更改,它不会更改已在窗口中的视图的外观。要更改当前在窗口中的视图的外观,请从视图层次结构中删除该视图,然后将其放回。

You can modify the tint color of the active navigation bar directly:

您可以直接修改活动导航栏的色调颜色:

self.navigationController?.navigationBar.tintColor = UIColor.init(red: 247/255, green: 119/255, blue: 0/255, alpha: 1)

As for why your tab bar isn't hiding, you are modifying your local variable, not the hidden property of the tab bar. You want:

至于您的标签栏没有隐藏的原因,您正在修改本地变量,而不是标签栏的隐藏属性。你要:

self.tabBarController?.tabBar.hidden = true

So your viewWillAppear should be something like:

所以你的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)

}