How to show sidebar in SwiftUI for macOS
Issue #710
Starting from macOS 11, we can use List
with SidebarListStyle
inside NavigationView
to declare master detail view. The SidebarListStyle
makes list translucent. It automatically handles selection and marks selected row in list with accent color.
1 | struct MainView: some View { |
If we use Section
instead of just Group we get a nice dropdown arrow button to expand and collapse section
1 | List { |
Show and hide side bar
To toggle side bar, we can use toggleSidebar
selector since for now, sidebar is backed by NSSplitViewController
1 | mainWindow.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil) |
We can specify tool bar items on either sidebar or content.
1 | .toolbar{ |
For tool bar to work, we must use App
and embed views inside WindowGroup
1 | @main |
Updated at 2021-01-06 20:43:40