I have a List
in a NavigationStack
and the list items have swipeActions
struct ListItem: Identifiable {
var id: String { return name }
let name: String
}
struct ContentView: View {
@Environment(\.editMode) var editMode
@State var listItems = [
ListItem(name: "Cow"),
ListItem(name: "Duck"),
ListItem(name: "Chicken")
]
var body: some View {
NavigationStack {
List(listItems) { listItem in
NavigationLink {
Text(listItem.name)
} label: {
Text(listItem.name)
}
.swipeActions(edge: .trailing) {
Button(action: { }, label: { Text("Edit") }).tint(.blue)
Button(role: .destructive, action: {}, label: { Text("Delete") })
}
}
.navigationTitle("Animals")
.navigationBarItems(trailing: EditButton())
}
}
}
For users who might have problems with the swipe gesture, I would like to have the Swipe Actions display when the list is in editMode
.
Is there a programmatic way to show the swipeActions
?
I have also tried using ForEach
with an onDelete
modifier. Then if you hit the edit button, it shows a circular button on the left for each row. If you click the circular button then it will show the swipe actions but I don't want the extra step of pressing the circular button.