WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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...


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.