Haensl
Haensl

Reputation: 475

How to know when SwiftUI presentation has finished?

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...

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

Answers (1)

Eric Yuan
Eric Yuan

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

Related Questions