Structure

ContextMenu

A container for views that you present as menu items in a contextual menu after completion of the standard system gesture. あなたがメニュー項目それらとして、あるコンテキストメニューの中に、標準システムジェスチャの完了の後に、提示するビューそれらに対するあるコンテナ。

Declaration 宣言

struct ContextMenu<MenuItems> where MenuItems : View

Overview 概要

A context menu view allows you to present a situationally specific menu to the user allowing them to perform actions relevant to the current task they’re performing in your app.

You create a context menu by first defining a ContextMenu container with the buttons that represent the actions a user can select from. Next, you add a contextMenu(_:) view modifier to the view that enables the context menu. The context menu view modifier takes the menu items you defined above as its argument.

The example below creates a four-item context menu container used by the context menu view modifier. The Boolean value shouldShowMenu controls the attachment of the context menu; it is set to true, enabling the contextual menu.

Note that it is possible to place the actions performed by the menu selection directly inside the button closures or, as shown below, to invoke them via function references.


func selectHearts() {
    // Act on hearts selection.
}
func selectClubs() { ... }
func selectSpades() { ... }
func selectDiamonds() { ... }


let menuItems = ContextMenu {
    Button("♥️ - Hearts", action: selectHearts)
    Button("♣️ - Clubs", action: selectClubs)
    Button("♠️ - Spades", action: selectSpades)
    Button("♦️ - Diamonds", action: selectDiamonds)
}


struct ContextMenuMenuItems: View {
    private var shouldShowMenu = true
    var body: some View {
        VStack {
            Text("Favorite Card Suit")
                .padding()
                .contextMenu(shouldShowMenu ? menuItems : nil)
        }
    }
}

A screenshot of a context menu showing four menu items: Hearts, Clubs,

Topics 話題

Creating a Context Menu コンテキストメニューを作成する

See Also 参照

Auxiliary View Modifiers