简体   繁体   English

如何正确隐藏/显示导航栏

[英]How to correctly hide/show the navigationBar

I have an app with some viewControllers. 我有一个带有一些viewControllers的应用程序。 Only one viewController must not display the navigationBar , so in this controller I have: 只有一个viewController不能显示navigationBar ,因此在此控制器中,我具有:

override func viewWillAppear(animated: Bool) {
      self.navigationController?.setNavigationBarHidden(true,animated:true)
}

Now, I noticed that this is a global setting because anytime I access another viewController the navigation bar is hidden. 现在,我注意到这是一个全局设置,因为每当我访问另一个viewController ,导航栏都是隐藏的。

So, in every viewController , in my viewWillAppear , I will make it visible again. 因此,在每个viewController ,在我的viewWillAppear ,我将使其再次可见。

But, is this the way to go? 但是,这是要走的路吗?

You can set the navigationBar to hidden in the viewWillAppear and to visible in the viewWillDisappear of this one viewController like that: 您可以设置的导航栏隐藏在viewWillAppear中,并在可见像,这其中的viewController的viewWillDisappear:

override func viewWillAppear(animated: Bool) {
  self.navigationController?.setNavigationBarHidden(true,animated:true)
}


override func viewWillDisappear(animated: Bool) {
  self.navigationController?.setNavigationBarHidden(false,animated:true)
}

In your UIViewController, if you are using a referencing outlet for the UINavigationBar, then you can change the state via the 'hidden' property, where myNavigationBar is the IBOutlet you defined: 在您的UIViewController中,如果您为UINavigationBar使用引用出口,则可以通过“ hidden”属性更改状态,其中myNavigationBar是您定义的IBOutlet:

override func viewWillAppear(animated: Bool) {
    self.myNavigationBar.hidden = true
}

Edit: seeing marc's answer, I am curious to know whether the property or the function is the preferred way to go? 编辑:看到马克的答案,我很好奇知道属性或函数是首选的方式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM