Kenny Kurochkin
Kenny Kurochkin

Reputation: 1108

SwiftUI - List editing mode - how to change delete button title?

Is there a way to change the delete button title when editing a List?

Example -


struct ContentView: View  {

    @State private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    Text(user)
                }.onDelete(perform: delete)
            }.navigationBarItems(trailing: EditButton())
        }
    }

    func delete(source: IndexSet) { }
}

enter image description here

Upvotes: 5

Views: 5471

Answers (2)

Hwangho Kim
Hwangho Kim

Reputation: 649

If you someone who want to adopt this below 15.0, try this.

For this, you need Introspect

List {
        ContentsView
        .introspectTableView { tv in
            tv.delegate = viewModel
        }
}

and ViewModel should be...

final class MyCustomViewModel: NSObject, ObservableObject, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "Kick Out!"
    }
}

enter image description here

Upvotes: 1

shraddha11
shraddha11

Reputation: 773

As of Xcode 11.3.1, SwiftUI doesn't support custom swipe actions for List items. Based on the history of Apple’s SDK evolution, we’re not likely to see support until the next major SDK version (at WWDC 2020) or later.

You would probably be better off implementing a different user interface, like adding a toggle button as a subview of your list item, or adding a context menu to your list item.

Note that you must be on beta 4 or later to use the contextMenu modifier on iOS.

See this - SwiftUI - Custom Swipe Actions In List

Upvotes: 3

Related Questions