1

I want to hide the navigationbar for only one viewcontroller which is the root viewcontroller of the UINavigationController.

Currently I am using below code to hide the navigation bar for a particular viewcontroller.

  • To hide the navigationbar,
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = true
        super.viewWillAppear(animated)
    }
  • To show the navigationbar for other viewcontrollers,
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
        super.viewWillDisappear(animated)
    }

When I am trying to use this code, the app is being crashed in iOS 13 devices because of threading violation: expected the main thread.

Please checkout the issue which I am getting when I use the above code to hide the navigationbar,

iOS 13: threading violation: expected the main thread

Please let me know if there is any other way to hide the navigationbar for only one viewcontroller.

Nikunj
  • 630
  • 1
  • 6
  • 20
  • You already asked this question once [here](https://stackoverflow.com/q/58335061/5215625). Don't ask multiple times about the same problem. Close this question. – Starsky Oct 11 '19 at 08:29

3 Answers3

9

I got the another way to hide/show navigationbar from one of my friend.

  • Set a delegate for the NavigationController:
navigationController.delegate = self
  • Hide/Show navigationbar for each ViewController all in one place
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    let hide = (viewController is YourVC)
    navigationController.setNavigationBarHidden(hide, animated: animated)
}
Nikunj
  • 630
  • 1
  • 6
  • 20
6
import UIKit
 class ViewController: UIViewController {

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

}

1

You can make it transparent (Completely invisible) when viewWillApper get called and back to normal when view willDisappear get called. Here are helper functions.


func makeNaBarTransparent() {
      navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
      navigationController?.navigationBar.shadowImage = UIImage()
      navigationController?.navigationBar.isTranslucent = true
  }


 func restoreNavigationBarToDefault() {
      navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
      navigationController?.navigationBar.shadowImage = nil
  }

USAGE

 import UIKit
class ViewController: UIViewController {

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

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

}
Mussa Charles
  • 4,014
  • 2
  • 29
  • 24