Generic Instance Method

contextMenu(menuItems:)

Attaches a contextual menu to the view.

Declaration

func contextMenu<MenuItems>(@ViewBuilder menuItems: () -> MenuItems) -> some View where MenuItems : View

Return Value

A view that adds a contextual menu to this view.

Discussion

Use contextual menus to add actions that change depending on the user’s current focus and task.

The following example creates text with a contextual menu:

func selectHearts() {
    // Act on hearts selection.
}
func selectClubs() { ... }
func selectSpades() { ... }
func selectDiamonds() { ... }

Text("Favorite Card Suit")
    .padding()
    .contextMenu {
        Button("♥️ - Hearts", action: selectHearts)
        Button("♣️ - Clubs", action: selectClubs)
        Button("♠️ - Spades", action: selectSpades)
        Button("♦️ - Diamonds", action: selectDiamonds)
}

See Also

Configuring Context Menu Views

func contextMenu<MenuItems>(ContextMenu<MenuItems>?) -> View

Attaches a contextual menu and its children to the view.