Reputation: 475
I want to know when my SwiftUI view has finished it's transition/animation in order to safely present additional views/modals.
Example:
I am in View A showing Sheet A. Within Sheet A I tap a button that dismisses Sheet A, transitions from View A to View B, and shows Sheet B on top of View B.
A naive implementation, e.g. initializing View B with a truthy binding/state to show Sheet B, will result in an error of the following form:
[Presentation] Attempt to present <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x107a03a00> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x107821800> (from <_TtGC7SwiftUI19UIHostingControllerVVS_19BridgedPresentation8RootView_: 0x107a1a200>) while a presentation is in progress.
This means we need to wait for View B to finish its transition before we can present Sheet B.
I am aware that I can...
NavigationStack/View
to the .sheet()
and just navigate from Sheet A to Sheet B. However, to provide the user with context, I want to have View B behind Sheet B.DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// trigger sheet
}
Waiting some seconds seems awefully non-deterministic to me, which is why I'm wondering whether there is a more elegant solution to this issue.
Upvotes: 10
Views: 558
Reputation: 1502
have you tried the onDisappear
method of View A? In my case, I found that onDisappear
of the previous presented view actually happens after onAppear
of the new view being presented.
Upvotes: 1