Declaration
compose().menuActionDismissBehavior(
behavior: SwiftUI.MenuActionDismissBehavior
)nonisolated func menuActionDismissBehavior(
_ behavior: MenuActionDismissBehavior
) -> some ViewParameters
behavior*SwiftUI.MenuActionDismissBehaviorThe menu action dismissal behavior to apply.
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.
- alertPresents an alert when a given condition is true, using a localized string resource for the title.
- confirmationDialogPresents a confirmation dialog when a given condition is true, using a localized string resource for the title.
- contextMenuAdds an item-based context menu to a view.
- dialogIconConfigures the icon used by dialogs within this view.
- dialogSuppressionToggleEnables user suppression of dialogs and alerts presented within self, with a custom suppression message on macOS. Unused on other platforms.
- dismissalConfirmationDialogPresents a confirmation dialog upon an attempt to dismiss the window for this view.
- documentBrowserContextMenuAdds to a DocumentLaunchView actions that accept a list of selected files as their parameter.
- fileDialogBrowserOptionsOn macOS, configures the fileExporter, fileImporter, or fileMover to provide a refined URL search experience: include or exclude hidden files, allow searching by tag, etc.
- fileDialogConfirmationLabelOn macOS, configures the fileExporter, fileImporter, or fileMover with a custom confirmation button label.
- fileDialogCustomizationIDOn macOS, configures the fileExporter, fileImporter, or fileMover to persist and restore the file dialog configuration.
- fileDialogDefaultDirectoryConfigures the fileExporter, fileImporter, or fileMover to open with the specified default directory.
- fileDialogImportsUnresolvedAliasesOn macOS, configures the fileExporter, fileImporter, or fileMover behavior when a user chooses an alias.