Search code examples
swiftuixcode13ios15

.confirmationDialog inside of .swipeActions does not work, iOS 15


With regards to iOS 15, Xcode 13; I am wondering if this is a bug, not properly implemented, or a planned non-functional feature...

With a list that has a .swipeActions that calls a .confirmationDialog the confirmation dialog does not show.

See example:

import SwiftUI

struct ContentView: View {
    
    @State private var confirmDelete = false
    
    var body: some View {
        NavigationView {
            List{
                ForEach(1..<10) {_ in
                    Cell()
                }

                .swipeActions(edge: .trailing) {
                    Button(role: .destructive) {
                        confirmDelete.toggle()
                    } label: {
                        Label("Delete", systemImage: "trash")
                    }

                    .confirmationDialog("Remove this?", isPresented: $confirmDelete) {
                        Button(role: .destructive) {
                            print("Removed!")
                        } label: {
                            Text("Yes, Remove this")
                        }
                    }
                }
            }
        }
    }
}

struct Cell: View {
    var body: some View {
        Text("Hello")
            .padding()
    }
}


Solution

  • Misconfiguration:

    The view modifier .confirmationDialog needs to be added to the view that is outside of the .swipeActions view modifier. It works when configured properly as shown below:

    import SwiftUI
    
    struct ContentView: View {
        
        @State private var confirmDelete = false
        
        var body: some View {
            NavigationView {
                List{
                    ForEach(1..<10) {_ in
                        Cell()
                    }
                    .swipeActions(edge: .trailing) {
                        Button(role: .destructive) {
                            confirmDelete.toggle()
                        } label: {
                            Label("Delete", systemImage: "trash")
                        }
                    }
                    //move outside the scope of the .swipeActions view modifier:
                    .confirmationDialog("Remove this?", isPresented: $confirmDelete) {
                        Button(role: .destructive) {
                            print("Removed!")
                        } label: {
                            Text("Yes, Remove this")
                        }
                    }
                }
            }
        }
    }
    
    struct Cell: View {
        var body: some View {
            Text("Hello")
                .padding()
        }
    }