I have a SwiftUI View that is presented by UIKit and is contained within a UIHostingController. This View has a button that should trigger an Alert to be presented. My implementation is straightforward. Here is an example:
struct ContentView: View {
@State private var presentAlert = false
var body: some View {
VStack {
Button("Delete") {
presentAlert = true
}
Spacer()
}
.alert(isPresented: $presentAlert) {
Alert(
title: Text("Are you sure?")
)
}.padding()
}
}
In iOS15 and above it works perfectly fine. However, my project has a minimum deployment target of iOS14 and it doesn't work in iOS14. Does anyone know of any work arounds?
I think this is caused by the same issue that prevents presentationMode from dismissing a SwiftUI View contained in a UIHostingController in iOS14. Here is a related question