Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a swipe-to-delete functionality in a SwiftUI List?

I just started working on couple of iOS Projects.

Currently I'm working on an iOS app using SwiftUI and I want to add a swipe-to-delete feature to a List view. I would like users to be able to swipe a row in the list to reveal a delete button, similar to the built-in swipe-to-delete behavior in the iOS Mail app.

I've tried searching for solutions and experimenting with onDelete, but I'm having trouble figuring out how to implement it correctly.

Could someone please provide guidance or point me in the right direction on how to achieve this swipe-to-delete functionality in SwiftUI? Any code examples or resources would be greatly appreciated.

I looked up online but was confused with solutions as many are in UIKit implementations and I have no idea about it.

Thank you in advance for your help!

like image 825
c-137 Avatar asked Oct 28 '25 03:10

c-137


1 Answers

Something like this?

NavigationStack {
        List {
            ForEach(items, id: \.self) { item in
                Text(item)
            }
            .onDelete(perform: delete)
        }
    }
}

func delete(at offsets: IndexSet) {
    items.remove(atOffsets: offsets)
}
like image 119
Jason Cheladyn Avatar answered Oct 29 '25 19:10

Jason Cheladyn