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

时间:2015-03-23 11:48:10

标签: ios swift uinavigationcontroller uinavigationbar

如何在第一个ViewController或swift中的特定ViewController中隐藏导航栏?

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

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

以及viewWillAppear

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

两种方法都隐藏了所有ViewControllers的导航控制器。

13 个答案:

答案 0 :(得分:238)

Swift 3

isInLobby

答案 1 :(得分:181)

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

在斯威夫特:

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)
}

答案 2 :(得分:74)

您可以在navigationController

中取消隐藏viewWillDisappear
override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    self.navigationController?.isNavigationBarHidden = false
}

Swift 3

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

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

答案 3 :(得分:17)

您还可以为此创建扩展,这样您就可以重复使用扩展,而无需在每个视图控制器中反复执行此操作。

import UIKit

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

    }

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

}

所以你可以使用下面的扩展方法

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

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

答案 4 :(得分:7)

在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;
}

答案 5 :(得分:2)

如何在Swift中显示导航栏:

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

答案 6 :(得分:1)

如何在Swift中隐藏导航栏:

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

答案 7 :(得分:0)

您可以从窗口控制器(Swift3)

执行此操作
class WindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        window?.titleVisibility = .hidden
    }
}

答案 8 :(得分:0)

  

在IOS 8中,这样做

navigationController?.hidesBarsOnTap = true

但仅当它是 UINavigationController

的一部分时 当你想要它时,

将它弄错

答案 9 :(得分:0)

我使用上面的变体,并将我的应用程序的各个部分隔离在不同的NavControllers中。这样,我不必重置可见性。例如,在启动序列中非常有用。

答案 10 :(得分:0)

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

答案 11 :(得分:0)

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

1.000000 1.000000

答案 12 :(得分:0)

    /*.  Swift 5  */     
    let controller =  self.storyboard?.instantiateViewController(withIdentifier: "sc_userNavigation") as! UserNavigationViewController
    let navigationController = UINavigationController(rootViewController: controller)
    navigationController.setNavigationBarHidden(true, animated: false)
    navigationController.modalPresentationStyle = .fullScreen
    self.present(navigationController, animated: false, completion: nil)