How it works
ButtonMenuStyle is the MenuStyle that draws a Menu as a single tappable button, which expands to reveal its contents when activated. It's the default presentation for menus on most platforms, giving you a self-contained control that shows its label up front and defers the list of actions until the user opens it. Reach for it when you want a compact menu trigger — a toolbar item, an inline "Options" control, or any place a button-shaped affordance reads more clearly than a bare disclosure.
Build the menu with
MenuButtonMenuStylestyles aMenu, so you start by declaring one with a label and a content closure of actions. In the example,Menu("Options")supplies the visible label, and the trailing closure holds the menu's commands — the menu is the view the style will reshape into a button.Populate the menu with
ButtonitemsThe menu's content is a set of controls the style presents once the button is opened. Here three
Buttonitems —"Rename","Duplicate", and a.destructive-role"Delete"— become the rows revealed on activation, whileButtonMenuStyleitself governs only the closed, button-shaped trigger.Apply the style with
.menuStyle(.button)You don't instantiate
ButtonMenuStyledirectly; you select it through themenuStyle(_:)modifier using the.buttonshorthand, which resolves toButtonMenuStyle. In the example,.menuStyle(.button)is attached to theMenu, telling SwiftUI to render the"Options"label as a button that discloses its items.Let the style propagate through the environment
menuStyle(_:)sets the style for the menu it's applied to and for any menus nested within that view hierarchy, so a single.menuStyle(.button)can govern a whole subtree. Surrounding layout — such as the.padding()that follows — only positions the resulting button and doesn't alter howButtonMenuStylepresents the menu.
.menuStyle(.button) for a different MenuStyle such as .borderlessButton (or remove the modifier entirely) to see how ButtonMenuStyle specifically frames the "Options" menu as a bordered button trigger.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 ButtonMenuStyleDemo: View {
var body: some View {
Menu("Options") {
Button("Rename", action: {})
Button("Duplicate", action: {})
Button("Delete", role: .destructive, action: {})
}
.menuStyle(.button)
.padding()
}
}