跳转至
Protocol

MenuStyle

A type that applies standard interaction behavior and a custom appearance to all menus within a view hierarchy.

Declaration

protocol MenuStyle

Overview

Use this protocol to create a componentized style to apply to a Menu.

Creating Your Own Style

There is only one requirement of conforming to the MenuStyle protocol: makeBody(configuration:). It takes a configuration parameter of type MenuStyleConfiguration which can be passed to Menu's init(_:) initializer:

struct RedBorderMenuStyle: MenuStyle {
    func makeBody(configuration: Configuration) -> some View {
        Menu(configuration)
            .border(Color.red)
    }
}
After creating a MenuStyle, you can apply it to a Menu using the menuStyle(_:) view modifier:

struct ButtonStyleView: View {
    var body: some View {
        Menu("PDF") {
            Button("Open in Preview", action: { })
            Button("Save as PDF", action: { })
        }
        .menuStyle(RedBorderMenuStyle())
    }
}

Pre-Packaged Styles

Existing menu styles include:

On iOS, BorderlessButtonMenuStyle is the DefaultMenuStyle, so there is no reason to use these.

Availability

iOS 14.0+

macOS 11.0+

Topics


Instance Method

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


Type Alias

Configuration The properties of a menu.


Associated Type

Body A view that represents the body of a menu.