5

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

2
  • Have you tried using UIViewControllerRepresentable / UIViewRepresentable? Commented Jan 19, 2020 at 20:29
  • duplicate, duplicate Commented Jan 20, 2020 at 6:51

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is this now possible as of XCode 12.3?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.