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

# contextMenu(menuItems:preview:)

Adds a context menu with a custom preview to a view.

```
nonisolated func contextMenu<M, P>(@ContentBuilder menuItems: () -> M, @ContentBuilder preview: () -> P) -> some View where M : View, P : View
```

## Parameters

`menuItems`

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

`preview`

A view that the system displays along with the menu.

## Return Value

A view that can display a context menu with a preview.

## Discussion

When you use this modifer to add a context menu to a view in your
app’s user interface, the system displays the custom preview beside the
menu.
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.

Define the custom preview by returning a view from the `preview`
closure. The system sizes the preview to match the size of its content.
For example, you can add a two button context menu to a [`Text`](/documentation/SwiftUI/Text) view,
and use an [`Image`](/documentation/SwiftUI/Image) as the preview:

```
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")
        }
    } preview: {
        Image("turtlerock") // Loads the image from an asset catalog.
    }
```

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

![A screenshot of a context menu with two buttons that are labeled](images/com.apple.SwiftUI/View-contextMenu-2-iOS@2x.png)

To customize the lift preview, shown while the system transitions
to show your custom `preview`, apply a [`contentShape(_:_:eoFill:)`](/documentation/SwiftUI/View/contentShape(_:_:eoFill:))
with a [`contextMenuPreview`](/documentation/SwiftUI/ContentShapeKinds/contextMenuPreview) kind. For example, you
can change the lift preview’s corner radius or use a nested view as the
lift preview.

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

If you don’t need a preview, use [`contextMenu(menuItems:)`](/documentation/SwiftUI/View/contextMenu(menuItems:))
instead. If you want to add a context menu to a container that supports
selection, like a [`List`](/documentation/SwiftUI/List) or a [`Table`](/documentation/SwiftUI/Table), and you want 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)