Is there a way to remove or deactivate swipe to delete functionality that remove items only per edit button?
-
@Yrb You should take a closer look at the question.Intronaen– Intronaen2021-01-15 23:55:02 +00:00Commented 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...Yrb– Yrb2021-01-15 23:58:07 +00:00Commented 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)George– George2021-01-22 00:07:38 +00:00Commented Jan 22, 2021 at 0:07
Add a comment
|
1 Answer
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)
}
}
6 Comments
Thomas
Hm, looks like this does not work for iOS 16 anymore, at least for the Beta. All elements stay undeletable, even in edit mode.
George
@Thomas Are you sure you have the
onDelete modifier added? If this exact code in entirety doesn’t work, then yeah - likely a bug.Thomas
Yes, just checked again. @George Don't know if it's a bug or will not work in iOS 16 anymore....
George
@Thomas You're right - very likely a bug somewhere with the updating of the
deleteDisabled modifier, because the editMode is still updating appropriatelydvdblk
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. |