2

Up until macOS Ventura, the following code would select the first item in the list (in the NavigationView). After I installed Ventura, it stopped. I tried recompiling the app, but nothing. Since the first item is not selected, the other view (TaskView) never gets loaded.

I spent the whole day searching but I can't find an answer. Is there a new way of selecting the first (or any) item in the list now?

Here's the code:

struct ContentView: View {        

    @State var selection: Set<Int> = [0]
        
    var body: some View {
        NavigationView {
            List(selection: self.$selection) {
                NavigationLink(destination: TasksView(byPriotity: 3)) {
                    Label("All Tasks", systemImage: "largecircle.fill.circle")
                }.tag(0) //<----- this
                
                Divider()
                
                NavigationLink(destination: TasksView(byPriotity: 0)) {
                    Label("Today", systemImage: "calendar")
                }
                
                NavigationLink(destination: TasksView(byPriotity: 1)) {
                    Label("Tomorrow", systemImage: "star.fill")
                }
             
            }
            .listStyle(SidebarListStyle())
            .frame(minWidth: 150, idealWidth: 150, maxWidth: .infinity, maxHeight: .infinity)
            
            TasksView(byPriotity: 3)
        }
    }
}
Aleph
  • 465
  • 2
  • 12
  • Start with `@State var selection: Set = []`. Try adding `.onAppear { selection = [0] }` to `NavigationView`. – vacawama Oct 26 '22 at 00:47
  • Thank you @vacawama. I just tried it with the same result. – Aleph Oct 26 '22 at 00:48
  • Give tags to the other links as well. Try `.onAppear { selection.insert(0) }` – vacawama Oct 26 '22 at 01:06
  • This is the first thing I tried, both setting tags for all and trying to force the value into `selection`. In any case, same result. Doesn't select. – Aleph Oct 26 '22 at 01:12

1 Answers1

0

The solution is

NavigationView {
   ...
}
.onAppear {
    DispatchQueue.main.async {
        selection = [0]
    }
}
Aleph
  • 465
  • 2
  • 12