Generic Instance Method


Attaches a contextual menu to the view.


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

Return Value

A view that adds a contextual menu to this view.


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")
    .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.