Home > Software design >  Select first item in a list on macOS Ventura
Select first item in a list on macOS Ventura

Time:10-29

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)
        }
    }
}

CodePudding user response:

The solution is

NavigationView {
   ...
}
.onAppear {
    DispatchQueue.main.async {
        selection = [0]
    }
}
  • Related