Xcode 11.2, Swift 5.1 Просто не предоставляйте onDelete в List, и не будет никаких кнопок Delete
Вот пример
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()
}
}
Альтернативный подход (с ограничениями)
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!)
}
}