TechnologiesSwiftUI

SidebarCommands struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A built-in set of commands for manipulating window sidebars.

How it works

SidebarCommands is a built-in Commands type that adds the standard system menu items for showing, hiding, and toggling a sidebar to your app's menu bar. Apps built around a NavigationSplitView get a leading sidebar column automatically, but users also expect to control its visibility from the View menu and with the matching keyboard shortcut; SidebarCommands supplies exactly those commands so you don't have to author them by hand. Reach for it in the commands builder of a Scene whenever your app presents a sidebar and you want the platform's familiar "Toggle Sidebar" behavior to appear and work consistently.

  1. Present a sidebar with NavigationSplitView

    SidebarCommands operates on the sidebar that a multi-column navigation container creates. Here the sidebar is the leading column of NavigationSplitView — a List(selection: $selection) of mailbox rows titled "Mailboxes" — paired with a detail column. This column is the surface whose visibility the commands will manage.

  2. Add the commands in a scene's commands builder

    SidebarCommands conforms to the Commands protocol, so you place it inside the .commands { } modifier on a Scene (typically the WindowGroup hosting SidebarCommandsDemo). Instantiating it with SidebarCommands() registers its menu items with the app; it carries no parameters because the items it provides are fixed by the platform.

  3. Get the standard View-menu items for free

    Once registered, SidebarCommands injects the system's sidebar entries — including "Toggle Sidebar" — into the View menu, complete with their conventional keyboard shortcut. These commands target the sidebar column produced by NavigationSplitView, so choosing them collapses or reveals the List of "Inbox", "Sent", and "Drafts" without any additional wiring.

  4. Compose it alongside other Commands

    Because it is an ordinary Commands value, SidebarCommands can sit next to other groups in the same commands builder — such as TextEditingCommands or your own CommandMenu — and SwiftUI merges them all into the menu bar. This lets you opt into the standard sidebar items while keeping full control over the rest of your app's menus.

Try it — Wrap SidebarCommandsDemo in a WindowGroup and attach .commands { SidebarCommands() }, then run the app and watch the "Toggle Sidebar" item appear in the View menu and collapse the List of mailboxes when invoked.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

SidebarCommands.swift
struct SidebarCommandsDemo: View {
    @State private var selection: String? = "Inbox"

    var body: some View {
        NavigationSplitView {
            List(selection: $selection) {
                Text("Inbox").tag("Inbox")
                Text("Sent").tag("Sent")
                Text("Drafts").tag("Drafts")
            }
            .navigationTitle("Mailboxes")
        } detail: {
            Text(selection ?? "Select a mailbox")
                .font(.title2)
                .padding()
        }
    }
}
Live preview
In… Se… Dr… Inb…
In… Se… Dr… Inb…
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →