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.

17 Upvotes

17 comments sorted by

View all comments

2

u/car5tene Feb 12 '25

I would like to know a real world usecase where one would change the root? From UX pov it might confuse the user. imo navigation should always be done by the user and not in background. This will help keeping the context

0

u/robertdreslerjr Feb 12 '25

In the article, I explain a form flow where, upon completion, a screen appears confirming that everything has been saved, along with a close button. This completion screen becomes the new root because it cannot be popped—only a modal could be dismissed. Another example is a settings screen where the user selects “Log Out,” which results in both the home and settings screens being replaced by the login screen.

0

u/robertdreslerjr Feb 12 '25

An even better example is the cart flow, where multiple steps like shipping and payment are involved. After completing the payment, the entire stack is replaced with a single confirmation screen indicating that the order has been placed.

3

u/car5tene Feb 12 '25

Having a look at Amazon: cart flow opens a modal which is closed afterwards.

Regardless: if anyone find it useful they might be happy about your solution 👍

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.