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

# contextMenu(_:)

Adds a context menu to the view.

```
nonisolated func contextMenu<MenuItems>(_ contextMenu: ContextMenu<MenuItems>?) -> some View where MenuItems : View
```

## Parameters

`contextMenu`

A context menu container for views that you
present as menu items in a context menu.

## Return Value

A view that can show a context menu.

## Discussion

Use this method to attach a specified context menu to a view.
You can make the context menu unavailable by conditionally passing `nil`
as the value for the `contextMenu`.

The example below creates a [`ContextMenu`](/documentation/SwiftUI/ContextMenu) that contains two items and
passes them into the modifier. The Boolean value `shouldShowMenu`,
which defaults to `true`, controls the context menu availability:

```
private let menuItems = 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")
    }
}

private struct ContextMenuMenuItems: View {
    @State private var shouldShowMenu = true

    var body: some View {
        Text("Turtle Rock")
            .contextMenu(shouldShowMenu ? menuItems : nil)
    }
}
```

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

---

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)