We also have this issue. Feedback submitted some time ago under FB13201571.
Editing works, as long as NavigationStack is not used. This works.
struct ListSelectionTest: View {
@State var selection: Set<String> = []
let strings = ["One", "Two", "Three"]
var body: some View {
VStack {
EditButton()
List(selection: $selection) {
ForEach(strings, id: \.self) { string in
Text(string).tag(string)
}
}
}
}
}
The problem appears when the List is wrapped in NavigationStack. This only works when the EditButton is tapped TWICE.
struct ListSelectionTest: View {
@State var selection: Set<String> = []
let strings = ["One", "Two", "Three"]
var body: some View {
NavigationStack {
VStack {
EditButton()
List(selection: $selection) {
ForEach(strings, id: \.self) { string in
Text(string).tag(string)
}
}
}
}
}
}
Replacing EditButton with a custom Button produces a different visual error. Editing is set correctly, but the multi-selection indicators quickly appear and then disappear again.
struct ListSelectionTest: View {
@Environment(\.editMode) private var editMode
@State var selection: Set<String> = []
let strings = ["One", "Two", "Three"]
var body: some View {
NavigationStack {
VStack {
Button((editMode?.wrappedValue.isEditing ?? false) ? "Done" : "Edit") {
editMode?.wrappedValue = (editMode?.wrappedValue.isEditing ?? false) ? .inactive : .active
}
List(selection: $selection) {
ForEach(strings, id: \.self) { string in
Text(string).tag(string)
}
}
}
}
}
}
In short, we suspect there is a SwiftUI bug related to triggering List editing when the list is in NavigationStack.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: