Couple questions about NavigationSplitView in SwiftUI

I'm in the process of building a SwiftUI app with a Sidebar and Detail View. I've run into some issues and I need some help fixing them:

  1. When the app is launched, my detail view shows up at the right of the sidebar. Great! However, the button that is supposed to navigate to that view isn't highlighted, which could cause user confusion. How do I make this button "light up" (with that blue background indicating to the user that it is selected, highlighted) and make the code so that this view opens up when the app opens (like in other Apple apps, see Music and Files)
  2. When I click on one of my sidebar items (which are just NavigationLinks), the background doesn't turn blue (highlight) to indicate that item is selected. I feel like this would cause user confusion, and I'd like to figure out why my code doesn't do this. One side note and a useful piece of information: whenever I click the NavigationLink in the sidebar, the console prints this: onChange(of: UpdateTrigger) action tried to update multiple times per frame.
  3. In the macOS app, upon launch, the detail view shows up. Great. What doesn't show up is my sidebar with the options on it. How do I make it so that the sidebar shows up no matter what unless the user specifies to remove it from view?

Attached are some images and my code. Thanks, y'all!

struct ContentView: View {

    var body: some View {

        

        // NavigationSplitView for the sidebar

        

        NavigationSplitView {

            

            // List of the options

            

            List {

                

                // NavigationLink for my button

                

                NavigationLink {

                    

                    // Linking to my main view that I want to show up upon launch of the app

                    

                    MainView()

                    

                    // Label to make it look pretty

                    

                } label: {

                    Label("Main View", systemImage: "icloud")

                }

                

                // Make the sidebar actually look like a sidebar

                

                .listStyle(.sidebar)

                

            }

            

            // NavigationTitle at the top of the sidebar

            

            .navigationTitle("Cling")

            

            //

            

        } detail: {

            

            // Make it so that the main screen shows up upon launch (however, this doesn't make the button light up to signify that the user has selected that view)

            

            MainView()

        }

    }

}

Selection:

  • You need maintain a @State variable for selection and pass it to List's selection parameter as a binding
  • To restore selection state across app launches use @SceneStorage
  • All you need is shown in the video link below, watch it slowly and understand the concepts

Console Warnings

  • onChange(of: UpdateTrigger) action tried to update multiple times per frame
  • A NavigationLink is presenting a value of type "Category" but there is no matching navigation destination visible from the location of the link. The link cannot be activated.
  • Feedback has been submitted for the above mentioned warnings and Apple Engineers are aware of this bug, so you can ignore it for now

MacOS Sidebar

  • You can tap on the sidebar button to open the sidebar
  • I think there is an option in NavigationSplitView configuration, you need to check the documentation columnVisibility but I haven't mentioned to get it working

Reference:

The NavigationSplitView works with List selection, and the NavigationLink must include the label and a value. The new process is explained here https://developer.apple.com/videos/play/wwdc2022/10054/

Although it works, there is an issue with the view at the moment. If you use an if else or a switch to select the view to show in Detail, it doesn't work. The solution for now is to embed it in a ZStack, as explained in this post https://developer.apple.com/forums/thread/707924

Couple questions about NavigationSplitView in SwiftUI
 
 
Q