5

How would remove the delete button in SwiftUI List rows when in Edit Mode? Note the hamburger button on the right of the row that allows rows to be re-ordered needs to continue to function.

Background - Want a list that has the "re-order" rows functions always enabled. Edit mode seems to enable this (i.e. leave List in edit mode) however do not want the red delete button on each row.

This is a SwiftUI specific question.

EDIT: After removing the delete button only here, so the swipe to delete still works...

3
  • Hi @Greg, did you find solution for your question ? Commented Jan 12, 2020 at 11:42
  • No. So I've just assumed it's not possible for the moment Commented Jan 14, 2020 at 1:58
  • Hi @Greg, seems this guy found the way, stackoverflow.com/a/58770635/5903004 Commented Jan 20, 2020 at 9:27

2 Answers 2

7

There is a modifier for that, just add '.deleteDisabled(true)'. You can also pass a variable into it making the delete disabled conditionally.

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

2 Comments

I was actually after how to remove the delete button only, so the swipe left to delete could still work - if that's possible?
I believe it is not possible (both in SwiftUI and UIKit), because swipe to delete gesture gets disabled in editing mode not to interfere with the dragging. The red minus button is there as a replacement for the swipe gesture. You could disabled delte in editing mode and leave it as an option otherwise.
1

Xcode 11.2, Swift 5.1 Just don't provide onDelete in List and there will be no Delete buttons

Here is example

no delete button

import SwiftUI
import Combine

struct ContentView: View {
    @State private var objects = ["1", "2", "3"]

    var body: some View {
        NavigationView {
            List {
                ForEach(objects, id: \.self) { object in
                    Text("Row \(object)")
                }
                .onMove(perform: relocate)
            }
            .navigationBarItems(trailing: EditButton())
        }
    }

    func relocate(from source: IndexSet, to destination: Int) {
        objects.move(fromOffsets: source, toOffset: destination)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Alternate approach (with limitations)

struct ContentView: View {
    @State private var objects = ["1", "2", "3"]
    @Environment(\.editMode) var editMode

    var body: some View {
//        NavigationView {
        VStack {
            // !!!  A. using NavigationView instead of VStack above does not work,
            // !!!  because editMode is not updated and always .inactive
            // !!!  B. Also it does not work in Preview, but works in run-time
            EditButton()
            List {

                ForEach(objects, id: \.self) { object in
                    Text("Row \(object)")
                }
                .onMove(perform: relocate)
                .onDelete(perform: delete)
                .deleteDisabled(disableDelete)
            }
//                .navigationBarItems(trailing: EditButton())
        }
    }

    var disableDelete: Bool {
        if let mode = editMode?.wrappedValue, mode == .active {
            return true
        }
        return false
    }

    func relocate(from source: IndexSet, to destination: Int) {
        objects.move(fromOffsets: source, toOffset: destination)
    }

    func delete(from source: IndexSet?) {
        objects.remove(atOffsets: source!)
    }
}

3 Comments

I was actually after how to remove the delete button only, so the swipe left to delete could still work - if that's possible?
I've found the approach, which gives visual behaviour like you want, BUT... there are limitations - read in code comments
Hi @Asperi, does this approach still work for you? I tried it in run time and didn't work. Also tried the new .swipeActions. Any suggestions? Thanks!

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.