<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 - 7.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/contextMenu(menuItems:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE11contextMenu9menuItemsQrqd__yXE_tAaBRd__lF"
  },
  "title" : "contextMenu(menuItems:)"
}
-->

# contextMenu(menuItems:)

Adds a context menu to a view.

```
nonisolated func contextMenu<MenuItems>(@ContentBuilder menuItems: () -> MenuItems) -> some View where MenuItems : 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 view that can display a context menu.

## Discussion

Use this modifier to add a context menu to a view in your app’s
user interface. Compose the menu by returning controls like [`Button`](/documentation/SwiftUI/Button),
[`Toggle`](/documentation/SwiftUI/Toggle), and [`Picker`](/documentation/SwiftUI/Picker) from the `menuItems` closure. You can also
use [`Menu`](/documentation/SwiftUI/Menu) to define submenus or [`Section`](/documentation/SwiftUI/Section) to group items.

The following example creates a [`Text`](/documentation/SwiftUI/Text) view that has a context menu
with two buttons:

```
Text("Turtle Rock")
    .padding()
    .contextMenu {
        Button {
            // Add this item to a list of favorites.
        } label: {
            Label("Add to Favorites", systemImage: "heart")
        }
        Button {
            // Open Maps and center it on this item.
        } label: {
            Label("Show in Maps", systemImage: "mappin")
        }
    }
```

When someone activates the context menu with an action like touch and
hold in iOS or iPadOS, the system displays the menu next to the content:

![A screenshot of a context menu showing two menu items: Add to](images/com.apple.SwiftUI/View-contextMenu-1-iOS@2x.png)

The system dismisses the menu if someone makes a selection, or taps
or clicks outside the menu.

To customize the default preview, apply a
[`contentShape(_:_:eoFill:)`](/documentation/SwiftUI/View/contentShape(_:_:eoFill:)) with a
[`contextMenuPreview`](/documentation/SwiftUI/ContentShapeKinds/contextMenuPreview) kind. For example, you can
change the preview’s corner radius or use a nested view as the preview.

> Note: This view modifier produces a context menu on macOS, but that
> platform doesn’t display a preview.

If you want to show a different preview, you can use
[`contextMenu(menuItems:preview:)`](/documentation/SwiftUI/View/contextMenu(menuItems:preview:)). To add a context menu to a
container that supports selection, like a [`List`](/documentation/SwiftUI/List) or a [`Table`](/documentation/SwiftUI/Table), and
to distinguish between menu activation on a selection and
activation in an empty area of the container, use
[`contextMenu(forSelectionType:menu:primaryAction:)`](/documentation/SwiftUI/View/contextMenu(forSelectionType:menu:primaryAction:)).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)