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

3      

@frios  

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

3      

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

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

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