contextMenu(_:)
Adds a context menu to the view.
Declaration
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 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)
}
}[Image]