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

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

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

以及viewWillAppear:

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

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


当前回答

在视图中调用set hide方法Will appear和Disappear。如果不调用,视图中的方法将消失,状态为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)
}

其他回答

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

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

在IOS 8中这样做

navigationController?.hidesBarsOnTap = true

但只有当它是UINavigationController的一部分时

当你想要回它时,把它设为false

在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;
}
     private func setupView() {
            view.backgroundColor = .white
            navigationController?.setNavigationBarHidden(true, animated: false)
        }

替代

在viewDidLoad中使用此设置

title = "Madman"
navigationController?.isNavigationBarHidden = false
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always

检查Collectionview, scrollview或tableView的约束

 NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])

斯威夫特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)
}