How it works
CommandGroup describes a related set of menu commands that you contribute to your app's main menu on macOS. SwiftUI organizes the standard menu bar into predefined groups — New, Open, Save, Undo, Cut/Copy/Paste, and so on — and CommandGroup lets you add your own buttons alongside those groups, replace a system group entirely, or insert items before or after one. You build commands as part of a Scene's commands modifier rather than inside a view hierarchy, so reach for CommandGroup whenever you need to extend or customize what appears in the menu bar.
Add commands in a Scene's
commandsmodifierMenu commands live at the scene level, not inside
body, so you attach them with the.commandsmodifier on aWindowGroupor otherScene. The closure is a command builder, and eachCommandGroupyou return contributes a block of menu items — as shown by the.commands { CommandGroup(...) }form in the example.Target a system group with a
CommandGroupPlacementEvery
CommandGroupinitializer takes aCommandGroupPlacementthat names the standard menu group to act on, such as.newItem,.saveItem, or.appInfo. The example passes.newItem, which addresses the New / Open area of the File menu so your commands land in a place users already expect.Replace, or insert before/after, the existing group
Three initializers control how your items combine with the system's:
CommandGroup(replacing:)swaps out the built-in commands for your own, whileCommandGroup(before:)andCommandGroup(after:)keep them and add your items adjacent. The example usesCommandGroup(replacing: .newItem)to take over the New entry completely.Supply the menu items in the content closure
The trailing closure is where you declare the actual menu entries, typically as
Buttons whose action runs your command and whose title becomes the menu label. The example providesButton("New") {}, which renders as a single "New" item in the File menu; add keyboard shortcuts with.keyboardShortcuton those buttons.
CommandGroup(replacing: .newItem) to CommandGroup(after: .newItem) and watch your "New" button appear alongside the system command instead of taking its place.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.
struct CommandGroupDemo: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("App Commands")
.font(.headline)
Text("CommandGroup adds items to the macOS menu bar inside a Scene's .commands modifier:")
.font(.caption)
.foregroundStyle(.secondary)
VStack(alignment: .leading, spacing: 6) {
Label("New", systemImage: "doc.badge.plus")
Label("Open\u{2026}", systemImage: "folder")
Divider()
Label("Export\u{2026}", systemImage: "square.and.arrow.up")
}
.padding(10)
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: 8))
Text(".commands { CommandGroup(replacing: .newItem) { Button(\"New\") {} } }")
.font(.system(.caption2, design: .monospaced))
.foregroundStyle(.blue)
}
.padding()
}
}