<!--
{
  "availability" : [
    "iOS: 13.0.0 - 27.0.0",
    "iPadOS: 13.0.0 - 27.0.0",
    "macCatalyst: 13.0.0 - 27.0.0",
    "macOS: 10.15.0 - 27.0.0",
    "visionOS: 1.0.0 - 27.0.0",
    "watchOS: 6.0.0 - 7.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ContextMenu",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI11ContextMenuV"
  },
  "title" : "ContextMenu"
}
-->

# ContextMenu

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

```
struct ContextMenu<MenuItems> where MenuItems : View
```

## Overview

A context menu view allows you to present a situationally specific menu
that enables taking actions relevant to the current task.

You can create a context menu by first defining a `ContextMenu` container
with the controls that represent the actions people can take,
and then using the [`contextMenu(_:)`](/documentation/SwiftUI/View/contextMenu(_:)) view modifier to apply the
menu to a view.

The example below creates and applies a two item context menu container
to a [`Text`](/documentation/SwiftUI/Text) view. The Boolean value `shouldShowMenu`, which defaults to
true, controls the availability of context menu:

```
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)

## Topics

### Creating a context menu

[`init(menuItems:)`](/documentation/SwiftUI/ContextMenu/init(menuItems:))

Creates a context menu.



---

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)