Hello,
I am creating a macOS app with SwiftUI and I added a sidebar with NavigationView and inside I added a List with NavigationLinks. I also added a separate NavigationLink at the bottom of the Window.
The behavior I want to have is, when I press the NavigationLink at the bottom, then to deselect all the other NavigationLinks in the sidebar List.
Most probably I still haven't properly understood SwiftUI and it's declarative paradigm yet, and I can't see a way to do this.
Could someone help me?
This is the code I have written:
Enum
Main Code
Sidebar List
I am creating a macOS app with SwiftUI and I added a sidebar with NavigationView and inside I added a List with NavigationLinks. I also added a separate NavigationLink at the bottom of the Window.
The behavior I want to have is, when I press the NavigationLink at the bottom, then to deselect all the other NavigationLinks in the sidebar List.
Most probably I still haven't properly understood SwiftUI and it's declarative paradigm yet, and I can't see a way to do this.
Could someone help me?
This is the code I have written:
Enum
Code Block enum Menu: String, CaseIterable { case library = "Library" case catalogue = "Catalogue" case filter = "Filter" }
Main Code
Code Block struct SplitView: View { var body: some View { NavigationView { VStack(alignment: .leading, spacing: nil) { MenuList() Divider() Spacer() NavigationLink(destination: FilterSpace()) { HStack { Text("") Text("Filter") } .padding(5.0) .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.gray, lineWidth: 1)) } .cornerRadius(10) .padding([.leading, .trailing], 7.0) .buttonStyle(BorderlessButtonStyle()) Spacer() } } .navigationViewStyle(DoubleColumnNavigationViewStyle()) .frame(minWidth: 800, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity) } }
Sidebar List
Code Block struct MenuList: View { @State private var selection: Menu? = .library var body: some View { List(selection: $selection) { Section(header: Text("Main Menu")) { NavigationLink(destination: LibrarySpace()) { Text(Menu.library.rawValue) .font(.subheadline) .padding(5) } NavigationLink(destination: CatalogueSpace()) { Text(Menu.catalogue.rawValue) .font(.subheadline) .padding(5) } } } .listStyle(SidebarListStyle()) .frame(minWidth: 150, maxWidth: 150) .onAppear { self.selection = .catalogue } } }
May have a look here on how to select an item.
https://stackoverflow.com/questions/60139184/swiftui-navigationlink-macos-default-selected-state
Similar principle should allow to deselect all as well.
https://stackoverflow.com/questions/60139184/swiftui-navigationlink-macos-default-selected-state
Similar principle should allow to deselect all as well.