如何在Swift中从第一个ViewController隐藏导航栏?


258

如何快速隐藏第一个ViewController或特定ViewController的导航栏?

我在下面使用了以下代码 viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.isNavigationBarHidden = true
}

还有 viewWillAppear

override func viewWillAppear(animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}

两种方法都对所有ViewController隐藏导航控制器。


1
您需要为所有
视图控制器

Answers:


320

如果您知道所有其他视图都应显示该栏,则可以使用 viewWillDisappear将其再次设置为可见。

在Swift中:

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

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

1
这个答案更有效。考虑添加的每个新ViewController的重复代码。stackoverflow.com/a/39679506/5079380
Amr ElAdawy

252

迅捷3

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

3
对于覆盖,不要忘了调用super方法:super.viewWillAppear(动画)和super.viewWillDisappear(动画)
NielsKoole

它会删除说回来的链接吗?
Cons Bulaquena

我坚信在视觉上与“向后滑动”效果不佳,但是一切都很好。谢谢!
tzaloga

旁注:self.不需要。
Fayer

向后滑动,从带有导航栏的视图到带有隐藏导航栏的视图,我们如何重新实现导航栏的淡入淡出?
T.Okahara

77

您可以取消隐藏navigationControllerviewWillDisappear

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

迅捷3

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

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

21

您也可以为此创建扩展,这样您就可以重复使用扩展,而无需在每个视图控制器中一次又一次地实现。

import UIKit

extension UIViewController {
    func hideNavigationBar(animated: Bool){
        // Hide the navigation bar on the this view controller
        self.navigationController?.setNavigationBarHidden(true, animated: animated)

    }

    func showNavigationBar(animated: Bool) {
        // Show the navigation bar on other view controllers
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }

}

因此,您可以使用以下扩展方法

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        hideNavigationBar(animated: animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        showNavigationBar(animated: animated)
    }

7
真的不值得扩展,是吗?:)
Joris Weimar

3
取决于您要隐藏/显示导航栏的视图数量。我觉得大多数情况下,您只隐藏第​​一个,但是如果您做得很多,扩展很好。
jnwagstaff '18

绝对不值得。不要发明已经存在的东西。
Thafer Shahin

8

在Swift 3中,您还可以使用isNavigationBarHidden属性来显示或隐藏导航栏

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Hide the navigation bar for current view controller
    self.navigationController?.isNavigationBarHidden = true;
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the navigation bar on other view controllers
   self.navigationController?.isNavigationBarHidden = false;
}

3

在Swift中显示导航栏的方法:

self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.navigationBar.isHidden = false
self.navigationController?.isNavigationBarHidden = false

3

在Swift中隐藏导航栏的方法:

self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true

1
self.navigationController?.setNavigationBarHidden(true,animation:true)为我工作
Nate Uni

0

在IOS 8中做到

navigationController?.hidesBarsOnTap = true

但仅当它是UINavigationController的一部分时

当您想要它时将其设置为假


0

我使用上述方法的变体,并隔离了要嵌入到不同NavController中的我的应用程序部分。这样,我不必重置可见性。例如,在启动序列中非常有用。


0
 private func setupView() {
        view.backgroundColor = .white
        navigationController?.setNavigationBarHidden(true, animated: false)
    }

0

在视图中调用set hide方法将出现和消失。如果您不调用视图中的方法,则状态为false时将消失,它将在完整的导航层次结构中隐藏导航栏

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

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

这是复制/粘贴响应。您的回答与这里的其他2或3个相等回答有什么区别?
Starsky

-3

您可以从窗口控制器(Swift3)进行操作

class WindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        window?.titleVisibility = .hidden
    }
}

4
什么是窗口控制器?
MBH

这是针对macOS,而不是iOS
Koen。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.