How it works
By default, choosing a command from a menu closes that menu. MenuActionDismissBehavior lets you override that default on a per-control basis, declaring whether a menu should dismiss once the user activates one of its actions. Reach for it when a menu hosts a control the user is likely to invoke repeatedly — such as a stepper, a counter, or a toggle — and you want the menu to stay open so they can keep interacting without reopening it each time.
Apply the behavior with menuActionDismissBehavior(_:)
The behavior is set through the
menuActionDismissBehavior(_:)view modifier, which attaches to the controls inside a menu's content. Placing it on a control declares how the surroundingMenushould respond when that control's action fires. In the example it is applied to eachButtonwithin theMenu("Add Items: \(count)")content.Keep the menu open with .disabled
The
.disabledvalue tells SwiftUI not to dismiss the menu after the action runs, so the control stays available for another tap. HereButton("Add One")uses.menuActionDismissBehavior(.disabled), letting the user incrementcountseveral times in a row while the menu remains on screen.Restore automatic dismissal with .enabled
The
.enabledvalue asks the menu to dismiss after the action completes — the standard menu behavior, stated explicitly. TheButton("Reset")applies.menuActionDismissBehavior(.enabled), so resettingcountto 0 also closes the menu, treating reset as a one-and-done command.Scope the choice per control
Because
MenuActionDismissBehavioris applied through a modifier on individual controls, a single menu can mix behaviors. The two buttons sharing oneMenudemonstrate this: the additiveAdd Onekeeps the menu open, while the terminalResetdismisses it, each declaring its own intent independently.
.menuActionDismissBehavior(.disabled) on the Add One button to .enabled and watch the menu close after a single increment instead of staying open for repeated taps.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 MenuActionDismissBehaviorDemo: View {
@State private var count = 0
var body: some View {
Menu("Add Items: \(count)") {
Button("Add One") { count += 1 }
.menuActionDismissBehavior(.disabled)
Button("Reset") { count = 0 }
.menuActionDismissBehavior(.enabled)
}
.padding()
}
}