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!
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With