|
I am an absolute beginner so explaining these concepts as if to an infant would be great. :) I've watched 3-4 tutorials now on NavigationStack but they all show simplistic examples in a single file. I am interested in understanding how you can add/remove views to a single NavigationStack across files and views. Here is the basic structure: ContentView.swift has my In However, now I am in the Or should it be passed around different views by a different mechanism? Is this what I have seen examples where a For example, in
And you can see there, I am sending However, then the preview inside
How am I supposed to get the Preview to know about the path? I read this excellent explanation on Previews by @Obelix https://www.hackingwithswift.com/forums/swiftui/binding-variable-won-t-preview/10974/10987 but remain confused as to what I should be giving the Preview to satisfy its needs? |
|
This is simplified example but basically you can see how it works. I hope comments are enough to understand what is going on. Just copy paste to see it in work.
so if you separate those structs out of this file #Preview will ask for parameters. As an example you can use .constat() to provide binding values for preview.
|
|
Thanks @ygeras will try this in the morning. If I had any hair left I would have torn it out today trying to get my head around this, this helps a lot. |
|
Related question: if I have a full screen menu that is brought in over the top, which is not part of the NavigationStack, and I click an item in this menu, how do I both dismiss the menu (which is done currently by the For example, I have been trying this:
But having an |
|
I have also tried this:
But notice that the |
|
I will continue using my example as snippets of your code do not show full picture what is going on in your particular case. You should understand that NavigationStack is not the same concept as poping up modal views such as (fullscreencover and sheet). I have added some functionality of fullscreencover and mark NEW PARTS with comments. So you can play around and see how modal views are controlled. It is usually @State var that is responsible for showing the view and not navigationpath as in NavigationStack.
|
|
Thanks again for looking at my issue. The key thing I need to do, is add to the NavigationStack, while simultaneously dismissing the fullScreenCover. So, imagine one view bringing in a menu as a fullScreenCover. In that full screen cover is a link to another view. I want to click that link to go to the new view, while dismissing the fullScreenCover at the same time (or at least from the same tap) Is that possible? |
|
Well I got this working but using something that is apparently deprecated. Is there an iOS 17 friendly way to do the same thing? Here is what I did, first added this function:
That part is now animating out my popover menu. Then inside my body view I am doing this to load the NavigationLink:
And that is pushing the SportSplash view into the NavigationStack. However, I have a warning that " Is this a horrible way to do this? |
|
I do not think this will work that way. I just modified the code in FullScreen view so you can play following two navigation paths as follows:
As you mentioned in your post you want it to look like this? MainPathStarts: View 1 -> View 2 -> SubPathStarts: Fullscreen then Dismiss -> X but how that view than could exist? View 3 My understanding is that SwiftUI using stack, that is it can push and pop the views that is LIFO cycle -> last in first out. So you want to break that rule that is not really possible (maybe there are some workarounds though). You push your View 1, then you push your View 2, you push your fullscreen, then you try to push View 3, but at the same time pop fullscreen, which breaks everthing. Using arrays for pushing views we can picture it like so:
Maybe for more visibility what is going on you can replace fullscreencover modifier with sheet and you will see how path follows its way and fullscreencover basically doing the same, only it covers all the screen.
|
|
Actually, seems like there is an easy workaround for that. If you modify fullscreencover to below code. It starts behaving this way that you're probably trying to achieve. So basically logic as follows: You pass into fullscreencover the path you're using for navigation tracking. Than add item to that pass via Button structure and then dismiss it. So this way it seems to be working just fine. Hopefully there is no conflict in order of execution in the rest of the code.
just a small note. it is not necessary to use |
SAVE 50% All our books and bundles are half price for Black Friday, 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.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.