Can anyone tell me how to disable the Delete swipe action for a list of items inside a NavigationView, while retaining the ability to delete list items when the NavigationView is in edit mode.
I have tried conditionally passing a closure or nil to the onDelete modifier, based on the editMode environment variable, but this doesn't seem to work for me. I also tried to use deleteDisabled, but Swift seems to ignore the change in my state variable, when passed to those modifiers.
Below is some sample code to reproduce the problem I have:
@State private var list = [1,2,3,4,5]
@State private var editMode = EditMode.inactive
var body: some View {
NavigationView {
List {
ForEach(list, id: \.self) { item in
Text("\(item) \(String(self.editMode.isEditing))")
}
.onDelete(perform: self.editMode.isEditing ? { index in list.remove(atOffsets: index) } : nil)
.deleteDisabled(!self.editMode.isEditing) // <-- Always disables delete, even in edit mode
}
.navigationBarItems(leading: EditButton(), trailing: Text(editMode == .active ? "Editing" : "Not Editing"))
.environment(\.editMode, $editMode)
}
}
Interestingly, the items in the list and the trailing Text in the navigation bar update just like they are supposed to whenever the editMode changes, but the change in editMode seems to be completely ignored by the onDelete and deleteDisabled modifiers, which appear to be in the same scope.
I am using Xcode 14.0 Beta.