How to hide a navigation bar from first ViewController in Swift?

前端 未结 12 1553
悲哀的现实
悲哀的现实 2020-11-30 17:03

How can I hide a navigation bar from first ViewController or a particular ViewController in swift?

I used the following code in viewDidLoad():

相关标签:
12条回答
  • 2020-11-30 17:06

    Swift 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)
    }
    
    0 讨论(0)
  • 2020-11-30 17:07

    If you know that all other views should have the bar visible, you could use viewWillDisappear to set it to visible again.

    In 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)
    }
    
    0 讨论(0)
  • 2020-11-30 17:08

    Ways to show Navigation Bar in Swift:

    self.navigationController?.setNavigationBarHidden(false, animated: true)
    self.navigationController?.navigationBar.isHidden = false
    self.navigationController?.isNavigationBarHidden = false
    
    0 讨论(0)
  • 2020-11-30 17:09

    You could also create an extension for this so you will be able to reuse the extension without implementing this again and again in every view controller.

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

    So you can use the extension methods as below

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            hideNavigationBar(animated: animated)
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            showNavigationBar(animated: animated)
        }
    
    0 讨论(0)
  • 2020-11-30 17:11
     private func setupView() {
            view.backgroundColor = .white
            navigationController?.setNavigationBarHidden(true, animated: false)
        }
    
    0 讨论(0)
  • 2020-11-30 17:11

    You can do it from the window controller (Swift3)

    class WindowController: NSWindowController {
    
        override func windowDidLoad() {
            super.windowDidLoad()
    
            window?.titleVisibility = .hidden
        }
    }
    
    0 讨论(0)
提交回复
热议问题