Contents

contextMenu(menuItems:)

Adds a context menu to a table row.

Declaration

@MainActor @preconcurrency func contextMenu<M>(@ViewBuilder menuItems: () -> M) -> ModifiedContent<Self, _ContextMenuTableRowModifier<M>> where M : View

Parameters

  • menuItems:

    A closure that produces the menu’s contents. You can deactivate the context menu by returning nothing from the closure.

Return Value

A row that can display a context menu.

Discussion

Use this modifier to add a context menu to a table row. Compose the menu by returning controls like Button, Toggle, and Picker from the menuItems closure. You can also use Menu to define submenus, or Section to group items.

The following example adds a context menu to each row in a table that people can use to send an email to the person represented by that row:

Table(of: Person.self) {
    TableColumn("Given Name", value: \.givenName)
    TableColumn("Family Name", value: \.familyName)
} rows: {
    ForEach(people) { person in
        TableRow(person)
            .contextMenu {
                Button("Send Email...") { }
            }
    }
}

If you want to display a preview beside the context menu, use contextMenu(menuItems:preview:). If you want to display a context menu that’s based on the current selection, use contextMenu(forSelectionType:menu:primaryAction:). To add context menus to other kinds of views, use contextMenu(menuItems:).

See Also

Adding a context menu to a row