1 | initial version |
To avoid automatic selection highlighting in nested lists in SwiftUI using the new NavigationLink API in iOS 16, you can add an explicit isActive
state variable for each nested list and only activate the child list when the parent is selected. Here's an example implementation:
struct ParentListView: View {
@State private var isChildListActive = false
var body: some View {
List {
NavigationLink("Parent Item 1", isActive: $isChildListActive) {
ChildListView()
}
NavigationLink("Parent Item 2", isActive: $isChildListActive) {
ChildListView()
}
}
}
}
struct ChildListView: View {
var body: some View {
List {
Text("Child Item 1")
Text("Child Item 2")
}
.navigationTitle("Child List")
.onAppear {
// Only activate the child list when the parent is selected
isChildListActive = true
}
.onDisappear {
// Deactivate the child list when navigating back to the parent
isChildListActive = false
}
}
}
In this example, the ParentListView
contains two NavigationLink
s to the ChildListView
. The ChildListView
only becomes active when the parent link is selected by setting the isChildListActive
state variable. When navigating back to the parent list, the isChildListActive
variable is set to false
to deactivate the child list. This approach avoids unwanted automatic highlighting of the child list when navigating to the parent view.