Ask Your Question
1

How can automatic selection highlighting be avoided in nested lists in SwiftUI using the new NavigationLink API in iOS 16?

asked 2021-06-05 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-06-01 00:00:00 +0000

ladyg gravatar image

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 NavigationLinks 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-05 11:00:00 +0000

Seen: 10 times

Last updated: Jun 01 '22