I am trying to keep the selection to remain highlighted when navigating back from the destination of the NavigationLink
. This code running on iPhone 14 simulator on iOS 16 deselects the current selection or selects 2 items and the count of selected items at the bottom does not match the highlighted items.
Am I missing something obvious ?
EDIT 1:
- Need to keep
NavigationView
for compatibility to iOS 15 - The issue is the same when using a single selection instead of a
Set
import SwiftUI
struct ContentView: View {
struct Ocean: Identifiable, Hashable {
let name: String
let id = UUID()
}
private var oceans = [
Ocean(name: "Pacific"),
Ocean(name: "Atlantic"),
Ocean(name: "Indian"),
Ocean(name: "Southern"),
Ocean(name: "Arctic")
]
@State private var multiSelection = Set<Ocean>()
var body: some View {
NavigationView {
VStack {
List(oceans, id: \.self, selection: $multiSelection) { ocean in
NavigationLink {
Text(ocean.name)
} label: {
Text(ocean.name)
}
}
Text("\(multiSelection.count) selected")
}
.navigationTitle("Oceans")
.toolbar { EditButton() }
}
}
}