iOS: Hide the navigation bar for only one viewcontroller which is root of the UINavigationController?
Asked Answered
A

3

3

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.

Affray answered 11/10, 2019 at 7:34 Comment(1)
You already asked this question once here. Don't ask multiple times about the same problem. Close this question.Belted
A
11

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)
}
Affray answered 12/10, 2019 at 4:40 Comment(0)
T
7
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
   }

}

Thromboplastin answered 11/10, 2019 at 9:8 Comment(0)
F
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()
    }

}
Franny answered 11/10, 2019 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.