I’m using a NavigationStack
and I’m dealing with the path
programmatically, so I’m always the one appending and removing entries from it to control navigation.
Any time I add or remove views from the path
, I get a nice transition that makes sense based on what happened in the path
. But things start to get interesting when I remove and add views at the same time:
- If after I add and remove views the
path
is larger than before, I get a transition as if I’m navigating forward, which makes a lot of sense. - If after I add and remove views the
path
is smaller than before, I get a transition as if I’m navigating backwards. This isn’t great because sometimes the user is actually navigating forward to a new screen, but they may have done something that caused all the screens behind to become irrelevant (e.g. after logging in), so sometimes it’s weird to get a backward transition. It would be much better if it understood the cases where the tip of thepath
is a new screen, thus it should be a normal forward transition regardless to what happened to the other screens in the path. - And finally, if the
path
remains with the same size after I add and remove views from it, then I get no transition at all. Even though the path changes, it’s like the implementation is like “well, it still has the same size, so it must be the same” even though that’s not the case.
Is there anything I can do to have more control over the transitions?
2
Answers
You have little control over the transitions. This question was about disabling the transitions, and even for that you need to rely on UIKit APIs.
That said, once we know how to disable transitions, we can force a "forward" transition. If you want to replace the top of the stack with
aNewValue
, or if you want to remove the top 2 items, then addaNewValue
, you can force a forward transition by doing the following steps:aNewValue
to the stackaNewValue
backe.g.
I can’t think of how you would force a "backward" transition with a type erased
NavigationPath
, but if your navigation path is a typed array, you canNavigationPath
s unfortunately)You’re talking about cyclic navigation. If you were just to keep appending to the path then it would work like a browser and Back would step back along exactly the same path. This would also resolve your transition issue. But if you want to keep the path optimized then one solution is to perform your own transitions (and in fact, your own navigation) once you are in the realm of the revolving views.
The following example is based on the solution I provided to SwiftUI bi-directional move transition moving the wrong way in certain cases. The views have an implicit hierarchy, so a lower view always slides in from the right and a higher view always slides in from the left. The Back button simply returns to the initial menu.