跳转至
Structure

DefaultMenuStyle

The default menu style, based on the menu's context.

Declaration

struct DefaultMenuStyle : MenuStyle

Overview

The default menu style can vary by platform. By default, macOS uses the bordered button style.

If you create a menu inside a container, the style resolves to the recommended style for menus inside that container for that specific platform. For example, a menu nested within another menu will resolve to a submenu:

Menu("Edit") {
    Menu("Arrange") {
        Button("Bring to Front", action: moveSelectionToFront)
        Button("Send to Back", action: moveSelectionToBack)
    }
    Button("Delete", action: deleteSelection)
}
You can override a menu's style. To apply the default style to a menu, or to a view that contains a menu, use the menuStyle(_:) modifier.

For example:

struct DefaultMenuView: View {
    var body: some View {
        Menu("PDF") {
            Button("Open in Preview", action: { })
            Button("Save as PDF", action: { })
        }
        .menuStyle(DefaultMenuStyle())
    }
}
A gif displaying a view containing a default style menu "PDF" that once clicked displays two buttons, "Open in Preview" and "Save as PDF", each with no action attached.

Availability

iOS 14.0+

macOS 11.0+

Topics


Instance Method

makeBody(configuration:) Creates a view that represents the body of a menu.


Initializer

init() Creates a default menu style.


Type Alias

Body A view that represents the body of a menu.