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

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

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

以及viewWillAppear:

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

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


当前回答

想在第一个视图控制器上隐藏导航栏

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

想在第二个视图控制器上显示导航栏

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

其他回答

    /*.  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)

你可以在viewWillDisappear中取消隐藏navigationController

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

你可以从窗口控制器(Swift3)来做

class WindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        window?.titleVisibility = .hidden
    }
}

想在第一个视图控制器上隐藏导航栏

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

想在第二个视图控制器上显示导航栏

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

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

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