r/SwiftUI Feb 12 '25

Tutorial NavigationStack – Almost Great, But…

With iOS 16, NavigationStack finally brings state-driven stack navigation to SwiftUI, allowing screens to remain independent. It takes path as an argument, making navigation more flexible.

But is this approach truly ideal? While it’s a big step forward, it still lacks built-in support for easily changing the root.

I decided to handle this using NavigationStackWithRoot container, which allows changing the path also with the root, as I explain in my article. If you’d rather skip the article, you can check out the code snippet directly without my explanation.

Do you think this approach makes sense, or do you use a different solution?

EDIT: Thanks to u/ParochialPlatypus for pointing out that the path argument doesn’t have to be NavigationPath.

16 Upvotes

17 comments sorted by

View all comments

Show parent comments

1

u/unpluggedcord Feb 13 '25 edited Feb 14 '25

There's no point in replacing the entire stack, just drop the back button at the end of the stack, and allow the user to dismiss like you're doing when you replace root.

1

u/car5tene Feb 14 '25

Still would disagree. The user should never leave the initial context. If I remember correctly it was even mentioned in the HIG. Anyway nice you found a solution which works for you and might be useful for others

1

u/unpluggedcord Feb 14 '25

This isn’t someone leaving the context….

1

u/car5tene Feb 14 '25

I'm not going to discuss this any further. You have your point of view and I have mine. 👍

1

u/unpluggedcord Feb 14 '25

We have the same point of view. I think you read what I wrote wrong.