Generic Instance Method


Adds a contextual menu to this 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:

Text("Control Click Me")
.contextMenu {
    Button(action: { print("added") } ) { Text("Add") }
    Button(action: { print("removed") } ) { Text("Remove") }

See Also

Configuring Context Menu Views

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

Attaches a ContextMenu and its children to self.


Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software