0

Is there a way to remove or deactivate swipe to delete functionality that remove items only per edit button?

3
  • @Yrb You should take a closer look at the question. Commented Jan 15, 2021 at 23:55
  • Yep, you are right. . . I COMPLETELY misread that one. That's what I get for doing this at the end of the day... Commented Jan 15, 2021 at 23:58
  • If my answer from last week helped, you can accept the answer so it easier for people to find this question in the future. (also you get some bonus reputation) Commented Jan 22, 2021 at 0:07

1 Answer 1

3

You can limit the delete functionality of a List/Form depending on the EditMode state, by using deleteDisabled(_:).

The following is a short example demonstrating deleting which only works in edit mode:

struct ContentView: View {
    
    @State private var data = Array(1 ... 10)
    
    var body: some View {
        NavigationView {
            Form {
                DataRows(data: $data)
            }
            .navigationTitle("Delete Test")
            .toolbar {
                EditButton()
            }
        }
    }
}


struct DataRows: View {
    
    @Environment(\.editMode) private var editMode
    @Binding private var data: [Int]
    
    init(data: Binding<[Int]>) {
        _data = data
    }
    
    var body: some View {
        ForEach(data, id: \.self) { item in
            Text("Item: \(item)")
        }
        .onMove { indices, newOffset in
            data.move(fromOffsets: indices, toOffset: newOffset)
        }
        .onDelete { indexSet in
            data.remove(atOffsets: indexSet)
        }
        .deleteDisabled(editMode?.wrappedValue != .active)
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hm, looks like this does not work for iOS 16 anymore, at least for the Beta. All elements stay undeletable, even in edit mode.
@Thomas Are you sure you have the onDelete modifier added? If this exact code in entirety doesn’t work, then yeah - likely a bug.
Yes, just checked again. @George Don't know if it's a bug or will not work in iOS 16 anymore....
@Thomas You're right - very likely a bug somewhere with the updating of the deleteDisabled modifier, because the editMode is still updating appropriately
This bug is very strange. I have made a custom edit button for testing purposes. It toggles a bool binding cellsDeletable and after a few seconds also changes the environment editMode to .active. The form rows / cells observe the state of the cellsDeletable value via .deleteDisabled(!cellsDeletable). During the time where the cellsDeletable is set to True and editMode hasn't yet been set to .active the cells can be deleted by a swipe. However as soon as the editMode is changed, the cells can't be deleted anymore.
|

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.