UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Using NavigationView conditionally

Forums > SwiftUI

I'm dealing with some extra space at the top of a view when using NavigationView, which appears is because you only need to use NavigationView once within all the views in the navigation stack. The problem is that the view with this issue only sometimes is loaded via a parent view that has NavigationView. It is also seen by itself directly loaded in the sheet() without the user having come through the parent view that has NavigationView.

How can I approach this? I guess I could pass in a value from the ParentView to the DetailView and detect if it exists and if/else using NavigationView there.. but that seems hacky?

There are 3 views. All are displayed inside sheets:

ParentView - has a wrapping NavigationView and the content is a loop of NavigationLinks in a list. (they load the destination with .navigationBarTitle("") .navigationBarHidden(true))

DetailView - This view is sometimes accessed directly, and sometimes by coming in via ParentView. This is the view where I have the issue. If I use NavigationView, that's the only way I can get a NavigationLink to let me navigate to DetailChildView. But doing so gives me a big gap at the top of my view (which I don't want to hack away with offset etc). If I don't use NavigationView - there is no gap of course, but I am unable to tap on a NavigationLink I have in this view.

DetailChildView - Just a basic destination view with no NavigationView or NavigationLink.

Edit:... I think I got this working by keeping the NavigationView inside my DetailView but placing .navigationBarHidden(true) and .navigationBarTitle("") after my inner ZStack, not on the NavigationView.. Will test more and see if that was indeed the fix



Did you figure this out? I have tried your solution, but unfortunately that will hide the toolbar that I have added.


The native navigation mechanism extends to all views inside a Parent wrapped to NavigationView and provides needed navigation out of the box... There is no need to create a NavigationView for each child view...

.navigationBarHidden(true) on 'Parrent View || Stack' It's enough.

And yes, you can create your own custom navigation... But that's another story...


Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.