menuActionDismissBehavior

Tells a menu whether to dismiss after performing an action.

View ModifieriOS 16.4+macOS 13.3+tvOS 16.4+watchOS 9.4+menuActionDismissBehavior(_:) in SwiftUI

Declaration

React
compose().menuActionDismissBehavior(
  behavior: SwiftUI.MenuActionDismissBehavior
)
Swift
nonisolated func menuActionDismissBehavior(
    _ behavior: MenuActionDismissBehavior
) -> some View

Parameters

Return value

A view that has the specified menu dismissal behavior.

Discussion

Use this modifier to control the dismissal behavior of a menu. In the example below, the menu doesn't dismiss after someone chooses either the increase or decrease action:

Menu("Font size") {
    Button(action: increase) {
        Label("Increase", systemImage: "plus.magnifyingglass")
    }
    .menuActionDismissBehavior(.disabled)

    Button("Reset", action: reset)

    Button(action: decrease) {
        Label("Decrease", systemImage: "minus.magnifyingglass")
    }
    .menuActionDismissBehavior(.disabled)
}

You can use this modifier on any controls that present a menu, like a Picker that uses the PickerStyle.menu style or a ControlGroup. For example, the code below creates a picker that disables dismissal when someone selects one of the options:

Picker("Flavor", selection: $selectedFlavor) {
    ForEach(Flavor.allCases) { flavor in
        Text(flavor.rawValue.capitalized)
            .tag(flavor)
    }
}
.pickerStyle(.menu)
.menuActionDismissBehavior(.disabled)

You can also use this modifier on context menus. The example below creates a context menu that stays presented after someone selects an action to run:

Text("Favorite Card Suit")
    .padding()
    .contextMenu {
        Button("♥️ - Hearts", action: increaseHeartsCount)
        Button("♣️ - Clubs", action: increaseClubsCount)
        Button("♠️ - Spades", action: increaseSpadesCount)
        Button("♦️ - Diamonds", action: increaseDiamondsCount)
    }
    .menuActionDismissBehavior(.disabled)

See also

More in Presentation.