I have a simple view where you can show an alert and then click the "Yes" button to dismiss it. The issue I’m having is that when tapping "Yes", the print()
statement fires only after the alert is fully dismissed, not immediately. I’m assuming this is caused by the default animation but not certain.
How can I make it fire the closure immediately upon tapping "Yes"?
import SwiftUI
struct ContentView: View {
@State private var show = false
var body: some View {
VStack {
Button("Show Alert") {
show = true
}
}
.alert("Cancel exposure?", isPresented: $show) {
Button("Yes", role: .destructive) {
print("Dismissing") // Should show immediately when tapping "Yes"
}
Button("No", role: .cancel) { }
}
}
}
#Preview {
ContentView()
}
2
Answers
You can try something like this with – onChanage..
This behavior is present in UIKit as well, you can see it if you change the alert to
fullScreenCover
And use this wrapper
An alternative is to use a
fullScreenCover
with Custom UI.Just as an added note, you can easily see this behavior if you use "Slow Animations" in the simulator.
This should also be submitted as a bug since both the SwiftUI and UIKit versions are behaving against documentation.