Search code examples
listbuttonswiftuiswipe-gesture

swipeActions in a list: button can not be pressed


I'm trying to add a simple swipe action to my list. But for some reason the button can not be pressed. When I perform a full swipe it works though.

This is my code:

var listView: some View {
    List {
        ForEach(Array(debits.enumerated()), id: \.element) { index, debit in
            HStack {
                Text(debit.name)
                    .swipeActions(allowsFullSwipe: true) {
                        Button(action: {
                            print("Hi \(index)")
                        }, label: {
                            Image(systemName: "slider.horizontal.3")
                        })
                    }
                Spacer()
                Text(String(debit.value))
                    .frame(width: 70, alignment: .trailing)
                Text("€")
                Divider().padding(.leading, 5)
                Toggle("", isOn: $debits[index].toggle)
                    .onChange(of: debit.toggle) { newValue in
                        calculateAvailable()
                    }
                    .frame(width: 50)
            }
        }
    }.listStyle(.plain).lineLimit(1)
}

Like I said, a press on the button does not print anything but the full swipe does.


Solution

  • I just found the answer. I used this code to dismiss the keyboard when tapping somewhere. This prevented the button from being pressed.

    var body: some View {
        VStack {
            my code
        }
        .onTapGesture {
            UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
    }