0

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?

MichaelGofron
  • 1,310
  • 4
  • 15
  • 29

2 Answers2

1

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.

Mercutio
  • 1,152
  • 1
  • 14
  • 33
  • I see, so is your recommendation then to have a navigation controller which embeds both `vcA` and `vcB` and have the navigation bar always hidden and then when we navigate to `vcB` we have a custom view that replicates the behavior of a navigation bar at the top of the view controller? – MichaelGofron May 10 '22 at 19:59
  • If you need to have the navigation controller in order to make it easier to pass a data model around that’s an option, but I’m saying vcA doesn’t need to be in the navVC and can just argue to it. – Mercutio May 10 '22 at 21:01
  • `vcA` should be in a navigation controller because I need a push animation between `vcA` and `vcB`. – MichaelGofron May 10 '22 at 22:41
0

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
}
Nandish
  • 1,136
  • 9
  • 16
  • Hm, that seems kind of fragile no? Like if you want to support dark mode you have to account for the different color it could be? As for why to hide the nav bar on vcA this vcA is embedded in a tabBarViewController. The `tabVC` is handling dismissal not the `vcA` – MichaelGofron May 10 '22 at 21:12
  • Thinking about this, I think this isn't a good solution. The bar would still be present and taking up space for one so it doesn't really answer the question. Second the buttons would still be tappable too so people would be able to commit actions for invisible bar items which seems like a bad user experience – MichaelGofron May 10 '22 at 21:41
  • In your question you haven't mentioned about tabVC. Why are you using both tabVC and navVC? Either one should be enough I guess. However, If you provide the code snippet as what you are doing so far and screenshot of the views you are working on would give more clarity. – Nandish May 11 '22 at 14:32
  • Yeah because the `tabVC` isn't really relevant to the question. I figured it would just complicate the explanation and the problem isn't with a tab view controller it's with different nav bar states in an embedded nav vc where one wants to show nav controls and the other doesn't – MichaelGofron May 11 '22 at 17:22