SwiftUI_visionOS_Note

.swipeActions()

May 8, 2024
1 min read|

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"
    ]
swift

Create a list of fruits

List {
	ForEach(fruits, id: \.self) { fruit in
		Text(fruit)
	}
		.onDelete(perform: delete)
}
swift
func delete(indexSeet: IndexSet) {...}
swift

$0 = Can use this instead of declaring name of content.

Swipe Action

Text(fruit)
	.swipeAction(
		edge: .trailing,
		allowsFullSwipe: true
	){
		Button("Archive"){...}
	}
swift

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"){...}
	}
swift

tint

Text(fruit)
	.swipeAction(
		...
	){
		Button("Archive"){...}.tint(.green)
		Button("Save"){...}.tint(.blue)
		Button("Junk"){...}.tint(.red)
	}
swift

Subscribe to our newsletter

Get the latest news and updates from our team