How to Hide a Navigation Bar from First Viewcontroller in Swift

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

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

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

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

hide Navigation Bar on root view controller and show it otherwise - Swift

I solved it with this code:

override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: animated);
super.viewWillDisappear(animated)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

How to hide the navigation bar shadow only in detail view?


For Generic flow

You could use this setup. Say ViewController is the all other view controller class (where you want the shadow), and DetailViewController is the detail view controller class.

The thing i'm doing preserving the shadow image.

ViewController.swift

class ViewController: UIViewController {

var shadowImage: UIImage!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

shadowImage = self.navigationController?.navigationBar.shadowImage
}


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

self.navigationController?.navigationBar.shadowImage = shadowImage
}

}

And DetailViewController.swift

class DetailViewController: UIViewController {


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

self.navigationController?.navigationBar.shadowImage = UIImage()
}
}

Storyboard setup
Sample Image

Output

Sample Image

Note: Another neat approach would be storing the shadow within the DetailsViewController and setting it while the view is about to disappear

class DetailsViewController: UIViewController {

var shadowImage: UIImage!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
shadowImage = self.navigationController?.navigationBar.shadowImage
self.navigationController?.navigationBar.shadowImage = UIImage()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = shadowImage
}
}

This solution is more elegant and resulting in a clean management.

For MasterDetailFlow, Using SplitViewController

In your MasterViewControlelr.swift

override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed // placeholder code when you created the project
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = nil
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = UIImage()
}

In your DetailViewController.swift

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = UIImage()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = nil
}

Output(Master/Detail flow)

Sample Image

Hide Navigation Bar for a View Controller

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

I think you have done mistake in animated: true

Hide iOS NavigationBar on one ViewController - weird animation happening

There's another method for hiding the navigation bar where you can set 'animated' to false which might help you.

self.navigationController?.setNavigationBarHidden(true, animated: false)

How can I hide navigation bar of a specific View Controller?

You are making mistake, in question you have set falsein viewDidLoad to hide navigationBar, you need to set true instead of false, also try on viewDidAppear.

self.navigationController?.setNavigationBarHidden(true, animated: true)  


Related Topics



Leave a reply



Submit