25
Apr
2022
SwiftUI Multiple Buttons in List Row
Reading time: 1 min
This quick recipe shows how to enable multiple Buttons in a List row. This is a long standing SwiftUI bug/quirk that fortunately has an easy workaround. Say you've put a few buttons in a List
row.
using code like this:
List {
HStack {
Button("First button") {
print("First action")
}
Button("Second button") {
print("Second action")
}
}
}
The bug lies in the fact that any tap, anywhere on the row, will trigger both button actions:
First action
Second action
The workaround is to apply buttonStyle
to the list. It can be any style other than .automatic
:
List {
HStack {
Button("First button") {
print("First action")
}
Button("Second button") {
print("Second action")
}
}
}
.buttonStyle(.borderless) // HERE