How it works
CompactMenuControlGroupStyle is a ControlGroupStyle that collapses a group of controls into a single, space-efficient menu. Instead of laying the group's controls out inline, it presents them behind one compact control that, when activated, reveals the members in a pop-up menu. Reach for it when a ControlGroup holds related actions that you want to keep available without dedicating a full row of toolbar or layout space to them. You apply it through the controlGroupStyle(_:) modifier using the type's matching .compactMenu static value rather than constructing the style directly.
Group related controls with ControlGroup
CompactMenuControlGroupStyle styles a ControlGroup, so the symbol only takes effect on the controls you bundle into one. In the example the group gathers three
Buttonactions —Cut,Copy, andPaste— which the compact-menu style will treat as the menu's members.Provide a label for the collapsed control
Because the style hides the members behind a single control, it needs something to show in the collapsed state. The trailing
label:closure supplies that affordance — here aLabel("Edit", systemImage: "pencil")— which becomes the tappable control that opens the menu.Apply the style with controlGroupStyle(.compactMenu)
You opt into CompactMenuControlGroupStyle through the
.controlGroupStyle(.compactMenu)modifier. The.compactMenustatic member resolves to this style, so SwiftUI renders the group as the compact label control plus a pop-up menu rather than as inline buttons.Inherit the menu presentation from the style
Once the style is attached, CompactMenuControlGroupStyle owns how the members appear: the
Cut,Copy, andPastebuttons surface as rows inside the menu that thepencillabel control presents, and each button'ssystemImage("scissors", "doc.on.doc", "doc.on.clipboard") carries through as the menu-row icon.
.controlGroupStyle(.compactMenu) for .controlGroupStyle(.navigation) and watch the same Cut/Copy/Paste group expand into inline controls instead of collapsing behind the Edit menu.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 CompactMenuControlGroupStyleDemo: View {
var body: some View {
ControlGroup {
Button("Cut", systemImage: "scissors") {}
Button("Copy", systemImage: "doc.on.doc") {}
Button("Paste", systemImage: "doc.on.clipboard") {}
} label: {
Label("Edit", systemImage: "pencil")
}
.controlGroupStyle(.compactMenu)
.padding()
}
}