Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

74
Vistas
SwiftUI dismiss alert by tapping outside of it

A similar question in the UIKit: How to dismiss UIAlertController when tap outside the UIAlertController?

I have a basic alert with a few buttons (cancel/delete style). I'd like to "dismiss" the alert by tapping outside of it, similarly to what would "cancel" button do:

struct ContentView: View {
    @State private var showingAlert = false

    var body: some View {
        Button("Show Alert") {
            showingAlert = true
        }
        .alert("Important message", isPresented: $showingAlert) {
            Button("First") { }
            Button("Second") { }
            Button("Third") { }
        }
    }
}

I tried adding onTapGesture handler, but it didn't help.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I am not sure if an alert is the right choice for what you want to achieve. Alerts are not intended to be used in that way you described and to dismiss an alert by tapping outside would feel awkward in regards to the user experience on iOS and is not encouraged by apple to do so. SwiftUI is intended to be used on multiple apple platforms like AppleWatch or AppleTV where you cannot simply tap outside of an alert. Therefore no such mechanisms like you described are implemented in SwiftUI.

A better approach would be to create your own view and use the ZStack. Then you can use the onTapGesture modifier on any clickable area to dismiss your view. Please check the example for a simplified example.

struct ContentView: View {
    @State private var showingAlert = false

    var body: some View {
        ZStack {
            // green area is clickable
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    Button("Show Alert") {
                                showingAlert = true
                            }
                    Spacer()
                }
               
                Spacer()
            }
            .background(.green)
            .onTapGesture {
                self.showingAlert = false
            }
            
            if(showingAlert) {
                // your overlay to be designed by you. 
                VStack {
                    Text("alert")
                }
                .frame(width: 100, height: 100, alignment: .center)
                .background(.gray)
            }
        }
       
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda