Home > Software design >  How to specify to show a navigation bar on one screen and not to show it on another?
How to specify to show a navigation bar on one screen and not to show it on another?

Time:05-11

I have two viewControllers, vcA and vcB. vcA is the initial view controller and it's embedded in a navigation controller, navVC. To display vcB I push vcB with navVC. However, for vcA I don't want to display the navigation bar and for vcB I do want to display the navigation bar. I've seen this answer but it doesn't solve my problem because I need to support backswipe. When I try to cause the navigation controller to disappear on backswipe the navigation controller immediately disappears or slowly disappears depending on if animation is enabled. My navVC is presenting with a page sheet animation as well so if I set the nav bar to disappear in viewWillDisappear it disappears as I modally pan down.

Is there a way to specify to not have a navigation bar shown on the first vc and have it shown on the second? Or is there some better way to do this I'm not familiar with?

CodePudding user response:

While the default view controllers make life really great initially, they usually come with very specific ways they operate and trying to operate outside those paradigms will be difficult if not impossible.

I would suggest not using the default navigation view controller on the view that you don’t want the bar to appear on and instead customize your own view to support the exact behavior you’re trying to replicate.

For example you can use a regular view without a bar at the top for viewA and then segue to the navigation view controller and make view B its default view.

CodePudding user response:

Why do you want to hide the navigation bar on vcA ?

Instead you can just make the barTintColor same as the vcA background color , you need not have to hide the navigation bar.

if #available(iOS 15.0, *) {
   let appearance = UINavigationBarAppearance()
   appearance.configureWithOpaqueBackground()
   appearance.backgroundColor = color
   vcA.navigationController?.navigationBar.standardAppearance = appearance
   vcA.navigationController?.navigationBar.scrollEdgeAppearance = vcA.navigationController?.navigationBar.standardAppearance
} else {
   vcA.navigationController?.navigationBar.barTintColor = color
}
  • Related