UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Sidebar losing selection

Forums > SwiftUI

I'm using a sidebar for navigation in my iPad app and it seems to loose its selection when I use another NavigationLink in the detail view.! Here's the sidebar code:

struct AppSidebarNavigation: View {
    @State private var selection: NavigationTag? = .calculator

    var body: some View {
        NavigationView {
            List {
                NavigationLink(
                    tag: NavigationTag.calculator,
                    selection: $selection,
                    destination: CalculatorView.init
                ) {
                    Label("Calculator", systemImage: "function")
                }

                NavigationLink(
                    tag: NavigationTag.tools,
                    selection: $selection,
                    destination: ToolsView.init
                ) {
                    Label("Tools", systemImage: "rectangle.3.group")
                }

                NavigationLink(
                    tag: NavigationTag.recents,
                    selection: $selection,
                    destination: RecentsView.init
                ) {
                    Label("Recents", systemImage: "clock")
                }
            }
            .listStyle(.sidebar)
            .navigationTitle("App Name")
        }
    }
}

Here's the detail view code:

struct GridToolsView: View {
    let tools: [Tool]

    var columns: [GridItem] {
        [GridItem(.flexible())]
    }

    var body: some View {
        LazyHGrid(rows: columns) {
            ForEach(tools) { tool in
                NavigationLink {
                    SelectedView(for: tool)
                } label: {
                    ToolView.gridItem(tool: tool)
                }
                .buttonStyle(.squishable(fadeOnPress: true))
            }
        }
        .frame(maxWidth: .infinity, alignment: .topLeading)
    }
}

Here's a video of the selected row losing its selection: Video

Does anyone know how to fix this problem?

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.