TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Why my SwiftUI Sidebar does not keep the item selection ?

Forums > SwiftUI

I'm asking your help today about a mystery behavior of the navigation link coupled with the sidebar on iPadOS.

First, here is my sidebar code:

NavigationView {
    List(1..<5) { _ in
        NavigationLink(destination: DetailView()) {
            Label("Item", systemImage: "house")
        }
    }
    .listStyle(SidebarListStyle())
}

This view contains navigation links to display a detail view as you can see.

My detail view contains another navigation link to a simple view containing a Text. Nothing more.

Here is the code of my detail view:

VStack {
    Spacer()

    NavigationLink(destination: Text("Hello")) {
        Text("Touch me")
    }

    Spacer()
}

So far, so good. Now, let's compile this code and see what's going on:

Great ! The sidebar is well displayed and the first item is selected. Now let's touching the Touch me button on the detail view to trigger a navigation link inside the detail view.

Oh no... When touching the button, the navigation link is triggered as wanted, but the item in the sidebar become unselected. Is there something I'm doing wrong ? I also tried to use the selection binding in the list but it doesn't change anything. To compare, I would like to reproduce the iPad Music app navigation of the Album item of the sidebar. In there, you can select an album and go deeper in the navigation stack. And it keeps the sidebar item selected !

Can you help me to fix this issue ?

Thanks a lot !

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.