如何隐藏特定View Controller的导航栏?

时间:2016-09-01 11:45:45

标签: ios swift

我的目的是设置一个ViewController(root),其中NavigationController将与三个ViewController相关联。

两个链接的ViewController必须在每个屏幕的顶部都有一个NavigationBar。另一个不必拥有Navigation bar。此外,根View Controller不必具有Navigation bar

我将NavigationBar隐藏在根View Controller上,如下所示:

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

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

但我无法隐藏不必拥有Navigation bar的关联View Controller上的Navigation bar

我还尝试使用viewDidLoad隐藏View Controller Navigation bar的{​​{1}}函数:

self.navigationController?.setNavigationBarHidden(false, animated: true)

Navigation bar仍在显示。

如何隐藏特定Navigation bar上的View Controller

提前致谢!

4 个答案:

答案 0 :(得分:2)

您可以尝试这样: -

override func viewWillAppear(animated: Bool) {
     super.viewWillAppear(animated)
    self.navigationController!.navigationBarHidden = true
}

override func viewWillDisappear(animated: Bool) {
  super.viewWillDisappear(animated)
    self.navigationController!.navigationBarHidden = false

}

答案 1 :(得分:0)

在viewDidAppear中尝试此代码: -

self.navigationController?.navigationBarHidden = true

答案 2 :(得分:0)

您犯了错误,有问题您已在false设置viewDidLoad隐藏navigationBar,您需要设置true而不是false,试试viewDidAppear

self.navigationController?.setNavigationBarHidden(true, animated: true)  

答案 3 :(得分:0)

在viewDidAppear方法

中使用以下代码
self.navigationController?.setNavigationBarHidden(true, animated: true)