TechnologiesSwiftUIPresentation and Dialogs

MenuActionDismissBehavior struct

iOSmacOStvOSwatchOSvisionOSiOS 16.4+✓ renders

The set of menu dismissal behavior options.

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.

  1. 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 surrounding Menu should respond when that control's action fires. In the example it is applied to each Button within the Menu("Add Items: \(count)") content.

  2. Keep the menu open with .disabled

    The .disabled value tells SwiftUI not to dismiss the menu after the action runs, so the control stays available for another tap. Here Button("Add One") uses .menuActionDismissBehavior(.disabled), letting the user increment count several times in a row while the menu remains on screen.

  3. Restore automatic dismissal with .enabled

    The .enabled value asks the menu to dismiss after the action completes — the standard menu behavior, stated explicitly. The Button("Reset") applies .menuActionDismissBehavior(.enabled), so resetting count to 0 also closes the menu, treating reset as a one-and-done command.

  4. Scope the choice per control

    Because MenuActionDismissBehavior is applied through a modifier on individual controls, a single menu can mix behaviors. The two buttons sharing one Menu demonstrate this: the additive Add One keeps the menu open, while the terminal Reset dismisses it, each declaring its own intent independently.

Try it — Change .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.

MenuActionDismissBehavior.swift
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()
    }
}
Live preview
Add Items: 0
Add Items: 0
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →