TechnologiesSwiftUI

CommandMenu struct

iOSmacOStvOSwatchOSvisionOS✓ renders

Command menus are stand-alone, top-level containers for controls that

How it works

CommandMenu declares a new, top-level menu in your app's main menu bar, sitting alongside the system-provided menus like File, Edit, and View. You add it to the commands modifier of a Scene so its title and contents become part of the menu bar that's always available while your app is frontmost, rather than living inside a particular window. Reach for CommandMenu when a group of commands is global to the app and deserves its own named menu — and pair the commands inside it with keyboard shortcuts so users can drive your app without the mouse.

  1. Give the menu a title with CommandMenu(_:)

    CommandMenu is initialized with a localized title string and a content closure; the title is the label that appears in the menu bar. In the example, the menu's items live in a Menu("File") content closure, the same trailing-closure pattern CommandMenu uses to declare its own entries.

  2. Populate it with menu-item views

    Inside the closure you compose ordinary command views — Buttons for actions and Dividers to group them. Here Button("New"), Button("Open"), and Button("Save") each carry an action that updates lastAction, while Divider() visually separates the saving command from the others; CommandMenu arranges these as the rows of its menu.

  3. Attach actions and run them from anywhere

    Each Button's action closure is the work the command performs when chosen. Because a CommandMenu belongs to the app rather than a single view, these closures typically mutate app- or scene-level state; in the example the buttons assign "New", "Open", and "Save" to the @State property lastAction, which the Text(lastAction) view then displays.

  4. Install it through the commands modifier

    A CommandMenu only takes effect when returned from a commands { } builder on a Scene such as WindowGroup. That placement is what promotes the menu out of the window content shown here — the VStack with its Text and Menu — and into the system menu bar where it stays reachable across windows.

Try it — Wrap one of the buttons, for example Button("Save"), with .keyboardShortcut("s") so choosing it from the menu — or pressing Command-S — fires the same action and updates lastAction.

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.

CommandMenu.swift
struct CommandMenuDemo: View {
    @State private var lastAction = "No action yet"
    var body: some View {
        VStack(spacing: 12) {
            Text("CommandMenu adds a top-level menu")
                .font(.headline)
            Text(lastAction)
                .foregroundStyle(.secondary)
            Menu("File") {
                Button("New") { lastAction = "New" }
                Button("Open") { lastAction = "Open" }
                Divider()
                Button("Save") { lastAction = "Save" }
            }
        }
        .padding()
    }
}
Live preview
CommandMenu adds a top-level menu No action yet File
CommandMenu adds a top-level menu No action yet File
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →