TechnologiesSwiftUI

ToolbarCommands struct

iOSmacOStvOSwatchOSvisionOS✓ renders

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

How it works

ToolbarCommands is a built-in command group that adds the standard toolbar menu items — commands like Show Toolbar and Customize Toolbar — to your app's menu bar. It conforms to the Commands protocol, so you supply it inside a scene's commands modifier rather than building those menu entries by hand. Reach for it when a window presents a toolbar and you want users to control that toolbar from the menu bar with the system-standard wording and key equivalents, instead of writing your own menu commands. Because the items are system-provided, they stay automatically in sync with the toolbar a window actually shows.

  1. Give a window a toolbar with the toolbar modifier

    ToolbarCommands operates on whatever toolbar your window content defines, so the toolbar comes first. In the example, the Text view inside NavigationStack attaches a .toolbar { ... } containing a ToolbarItemGroup(placement: .primaryAction) with bold, italic, and underline Buttons — that group is the toolbar the menu commands will reference.

  2. Add ToolbarCommands to a scene's command set

    Instantiate it with ToolbarCommands() and pass it to the commands modifier on a WindowGroup (or other scene). Doing so installs the View menu's toolbar entries; nothing in the toolbar declaration changes, and you write no Button or CommandMenu of your own for these items.

  3. Let the system populate the standard menu items

    Once added, ToolbarCommands contributes platform-standard entries such as Show/Hide Toolbar and Customize Toolbar, complete with their conventional titles and key equivalents. These act on the focused window's toolbar — the one holding the .primaryAction group with the bold/italic/underline buttons — without any manual wiring.

  4. Compose it with other command groups

    As a Commands value, ToolbarCommands can sit alongside other groups (for example via SidebarCommands or your own CommandGroup) within a single commands modifier. SwiftUI merges them into the menu bar, placing the toolbar items in their expected location relative to the rest.

Try it — Wrap the NavigationStack window in a WindowGroup { ... }.commands { ToolbarCommands() }, run on macOS, and watch the View menu gain Show Toolbar and Customize Toolbar items that drive the same toolbar holding the bold/italic/underline buttons.

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.

ToolbarCommands.swift
struct ToolbarCommandsDemo: View {
    @State private var bold = false

    var body: some View {
        NavigationStack {
            Text(bold ? "Bold On" : "Bold Off")
                .font(.title2)
                .fontWeight(bold ? .bold : .regular)
                .padding()
                .navigationTitle("Editor")
                .toolbar {
                    ToolbarItemGroup(placement: .primaryAction) {
                        Button(action: { bold.toggle() }) {
                            Image(systemName: "bold")
                        }
                        Button(action: {}) {
                            Image(systemName: "italic")
                        }
                        Button(action: {}) {
                            Image(systemName: "underline")
                        }
                    }
                }
        }
    }
}
Live preview
Bold Off 9:41 Editor
Bold Off 9:41 Editor
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →