SwiftUI swipeActions modifier in iOS 15

Santosh Botre
3 min readJun 15, 2021

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 Button instances.

edge The edge of the view to associate the swipe actions with. The default is HorizontalEdge.trailing.

allowsFullSwipeA Boolean value that indicates whether a full swipe automatically performs the first action. The default is true.

Before directly jumping into a View. Let’s have data to be populated into a View.

Let’s build a list view to populate the WWDC21 sessions videos.

Session List View

Just add the newly introduced swipeActions modifier in SwiftUI as shown below.

Session List View with swipeActions added to it

I thought its done!!! But when I examined under the canvas preview. It’s not working.

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,

  1. Swipe gesture is not working
  2. Swipe action is not attached to the list
  3. I have no clue what went wrong?
  4. Its in beta.
GIf!!

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.

It started working!!!

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,

--

--

Santosh Botre

Take your time to learn before develop, examine to make it better, and eventually blog your learnings.