---
title: "contextMenu(_:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/contextmenu(_:)"
---

# contextMenu(_:)

Adds a context menu to the view.

## Declaration

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

Return Value A view that can show a context menu.

## Discussion

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

## See Also

### Auxiliary view modifiers

- [navigationBarTitle(_:)](swiftui/view/navigationbartitle(_:).md)
- [navigationBarTitle(_:displayMode:)](swiftui/view/navigationbartitle(_:displaymode:).md)
- [navigationBarItems(leading:)](swiftui/view/navigationbaritems(leading:).md)
- [navigationBarItems(leading:trailing:)](swiftui/view/navigationbaritems(leading:trailing:).md)
- [navigationBarItems(trailing:)](swiftui/view/navigationbaritems(trailing:).md)
- [navigationBarHidden(_:)](swiftui/view/navigationbarhidden(_:).md)
- [statusBar(hidden:)](swiftui/view/statusbar(hidden:).md)
