<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TabContent/contextMenu(menuItems:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI10TabContentPAAE11contextMenu9menuItemsQrqd__yXE_tAA4ViewRd__lF"
  },
  "title" : "contextMenu(menuItems:)"
}
-->

# contextMenu(menuItems:)

Adds a context menu to a tab.

```
nonisolated func contextMenu<M>(@ContentBuilder menuItems: () -> M) -> some TabContent<Self.TabValue> where M : 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 row that can display a context menu.

## Discussion

Use this modifier to add a context menu to a tab’s sidebar representation.
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 adds the ability to pin the tab or share
the tab’s books with a friend.

```
Tab("Currently Reading", systemImage: "book") {
    CurrentBooksList()
}
.contextMenu {
    Button {
        // Pin this tab.
    } label: {
        Label("Pin", systemImage: "pin")
    }
    Button {
        // Open a share sheet to share
    } label: {
        Label("Share", systemImage: "square.and.arrow.up")
    }
}
```

---

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)