Previously, we List rows were limited to only 1 swipe action - swipe to delete.
Now, however, we can totally customize these rows and add a whole bunch of buttons to them.
We will implement a few different variations of list row actions, each with different colors, titles, and swiping from both the leading and the trailing edge.
$0 = Can use this instead of declaring name of content.
Set up
Create a array of string named fruits
@State var fruits: [String] = [
"apple", "orange", "banana", "peach"
]
Create a list of fruits
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
.onDelete(perform: delete)
}
func delete(indexSeet: IndexSet) {...}
$0 = Can use this instead of declaring name of content.
Swipe Action
Text(fruit)
.swipeAction(
edge: .trailing,
allowsFullSwipe: true
){
Button("Archive"){...}
}
by adding .swipeAction
, we can add a content to swipe.
edge means where the action will occur, on default, it’s trailing
allowFullSwipe means, it can be swiped fully to execute the action
Just like the button we add the content at the end
.swipeAction(…){content})
adding multiple content
Text(fruit)
.swipeAction(
...
){
Button("Archive"){...}
Button("Save"){...}
Button("Junk"){...}
}
tint
Text(fruit)
.swipeAction(
...
){
Button("Archive"){...}.tint(.green)
Button("Save"){...}.tint(.blue)
Button("Junk"){...}.tint(.red)
}