Hi, I am building a view containing a list of struct objects. I have implemented the onMove to allow the list rows to be moved together with the toolbar EditButton. This works but I have a problem as per title, even when the EditButton is not clicked, i.e., not in editing mode, I still can move the list rows by long pressing the rows. I have searched high and low for a solution but couldn't find one that works perfectly.
Below is the code snippet.
I am a new iOS dev and really appreciate if someone could help me.
Many thanks!
@AppStorage("fruits") private var fruits = Fruits.fruits
var body: some View {
NavigationStack {
VStack {
List {
ForEach(fruits) { fruit in
NavigationLink(destination: FruitsView(title: fruit.title)) {
fruit.icon
Text(fruit.title)
}
}
.onMove { fruits.move(fromOffsets: $0, toOffset: $1) }
}
.environment(\.defaultMinListRowHeight, UIElementConstants.listCellHeight)
}
.toolbar { EditButton() }
.navigationTitle("Fruits")
}
}
struct Fruits: Identifiable, Codable {
var id: UUID
var title: String
var iconName: String
var icon: Image {
Image(systemName: self.iconName)
}
init(id: UUID = UUID(), title: String, iconName: String) {
self.id = id
self.title = title
self.iconName = iconName
}
}
extension Fruits {
static let fruits: [Fruits] = [
Fruits(
title: "Apple",
iconName: "basket.fill"),
Fruits(
title: "Banana",
iconName: "basket.fill"),
Fruits(
title: "Pineapple",
iconName: "basket.fill")
]
}
@eugeneee good catch!
Sorry about that, apply this modifier to your NavigationLink
:
.moveDisabled(editMode?.wrappedValue == EditMode.inactive)
You'll need to read the environment value with @Environment(\.editMode) private var editMode
for this to work.
One other thing - there is a small known issue with editMode in which it won't work in a NavigationStack unless the structure of your app is the following:
NavigationStack {
VStack {
ContentView()
}
}
where ContentView is
struct ContentView: View {
@Environment(\.editMode) private var editMode
var body: some View {
/// List etc
}
}