I have scenario where I need to display an alert without any button. Is it possible to remove the buttons from the default Alert in SwiftUI?
I know how to create a custom view as Alert. But I would love to use the default one instead of creating a custom one.
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
}

No, the
alertview modifier cannot create alerts without any buttons. The descriptions of the initialisers forAlertare:All of these create alerts with buttons.
Note that
Alertis deprecated since iOS 16.4. Now you should use one of the newalertmodifiers like this one.However, the new
alertmodifiers cannot create alerts without buttons either. All of their documentation mentions that: