I couldn't find any reference about any ways to make a pop or a dismiss programmatically of my presented view with SwiftUI.
Seems to me that the only way is to use the already integrated slide dow action for the modal(and what/how if I want to disable this feature?), and the back button for the navigation stack.
Does anyone know a solution? Do you know if this is a bug or it will stays like this?
The core concept of SwiftUI is to watch over the data flow.
You have to use a
@State
variable and mutate the value of this variable to control popping and dismissal.Alternatively, if you don't want to do it programatically from a button, you can emit from the view model whenever you need to pop. Subscribe to a @Published that changes the value whenever the saving is done.
SwiftUI Xcode 11.0
First, declare the @Environment which has a dismiss method which you can use anywhere to dismiss the view.
There is now a way to programmatically pop in a NavigationView, if you would like. This is in beta 5. Notice that you don't need the back button. You could programmatically trigger the showSelf property in the DetailView any way you like. And you don't have to display the "Push" text in the master. That could be an EmptyView(), thereby creating an invisible segue.
You can try using a custom view and a
Transition
.Here's a custom modal.
I reused the
Transition.moveAndFade
from the Animation Views and Transition tutorial.It is defined like this:
You can test it - in the simulator, not in the preview - like this:
Thanks to that transition, you will see the modal
sliding in from the trailing edge
, and the it willzoom and fade out when it is dismissed
.I experienced a compiler issue trying to call
value
on the presentationMode binding. Changing the property towrappedValue
fixed the issue for me. I'm assumingvalue
->wrappedValue
is a language update. I think this note would be more appropriate as a comment on Chuck H's answer but don't have enough rep points to comment, I also suggested this change as and edit but my edit was rejected as being more appropriate as a comment or answer.