iosswiftxcodeswift4xcode9

Hide Navigation Bar for a View Controller


I've tried to hide the navigation controller for a single view controller with no luck, the navigation bar is hidden for the first vc, but it's not displaying for the second vc.

Here's the code I've used in the first vc:

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

    // Hide the Navigation Bar
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

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

    // Show the Navigation Bar
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

What's changed in swift 4? That code worked in swift 3...


Solution

  • Use code:- Swift 5

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        // Hide the Navigation Bar
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
        // Show the Navigation Bar
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }