Why does a swipe action (using the new .swipeAction in SwiftUI 5.5, currently beta 3) set editMode? Notice when you swipe that "Edit" changes to "Done"?
And yet this is not like full edit mode because tapping the EditButton shows the move handles (and the ever-annoying ugly blank indentation for the absent onDelete modifier).
I think my next project won't use SwiftUI.
import SwiftUI
struct Fruit: Identifiable {
let id: UUID = UUID()
let name: String
let color: Color
}
struct ListingView: View {
@State var fruits: [Fruit] = [
Fruit(name: "banana", color: .yellow),
Fruit(name: "apple", color: .green),
Fruit(name: "tomato", color: .red)]
@Environment(\.editMode) private var editMode
var body: some View {
NavigationView {
VStack(alignment: .leading) {
List {
ForEach(fruits) { fruit in
NavigationLink(
destination: ZStack {
fruit.color
Text(fruit.name).bold().font(.largeTitle)
}
.navigationTitle(fruit.name),
label: {
HStack(alignment: .center) {
fruit.color.frame(width: 30, height: 30)
Text(fruit.name)
}
})
.swipeActions(edge: .leading, allowsFullSwipe: false) {
Button(action: { delete(fruit) },
label: { Image(systemName: "trash") }).tint(.red)
}
}
.onMove(perform: { from, to in
fruits.move(fromOffsets: from, toOffset: to)
})
}
}
.navigationBarTitle("Fruits")
.toolbar {
ToolbarItem(placement: .primaryAction) {
EditButton()
}
}
}
}
private func delete(_ fruit: Fruit) {
guard let index = fruits.firstIndex(where: {$0.id == fruit.id}) else { return }
fruits.remove(at: index)
}
}
struct ListingView_Previews: PreviewProvider {
static var previews: some View {
ListingView()
}
}