In iOS 15, SwiftUI added a new swipeActions modifier.
It allows you to adds custom swipe actions to a row in a list.
func swipeActions<T>(edge: HorizontalEdge = .trailing, allowsFullSwipe: Bool= true, content: () -> T) -> some View where T : View
We can use this method to add swipe actions to a view that acts as a row in a list. Indicate the
HorizontalEdge where the swipe action originates, i.e., .leading or .trailing and define our individual actions with
edgeThe edge of the view to associate the swipe actions with. The default is
allowsFullSwipeA Boolean value that indicates whether a full swipe automatically performs the first action. The default is
Before directly jumping into a View. Let’s have data to be populated into a View.
Just add the newly introduced swipeActions modifier in SwiftUI as shown below.
I thought its done!!! But when I examined under the canvas preview. It’s not working.
Then run the simulator as well as on the iOS 15 beta device. But no luck.
I thought possible issues might be,
- Swipe gesture is not working
- Swipe action is not attached to the list
- I have no clue what went wrong?
- Its in beta.
I went through swipeActions discussion and code sample by Apple. Found out that instead of using the List container that presents rows of data arranged in a single column. They have used the ForEach — A structure that computes views on demand from an underlying collection of identified data.
Wala!!! It worked.
All my assumptions around the possible issues where incorrect.
NOTE: Not sure why it is not working when you directly use List container. As List allow us to create lists dynamically from an underlying collection of data.
I will update the blog as soon I get to know the reason. In case, you know the possible reason, I will appriciate your comment on the blog.
Thank you in advance!!!
Question is been asked on Stackoverflow.
Other SwiftUI 15 blogs you might want to take a look,