Structure

Menu

A control for presenting a menu of actions. アクションいくつかからなるあるメニューを提示するためのコントロール。

Declaration 宣言

struct Menu<Label, Content> where Label : View, Content : View

Overview 概要

The following example presents a menu of three buttons and a submenu, which contains three buttons of its own.


Menu("Actions") {
    Button("Duplicate", action: duplicate)
    Button("Rename", action: rename)
    Button("Delete…", action: delete)
    Menu("Copy") {
        Button("Copy", action: copy)
        Button("Copy Formatted", action: copyFormatted)
        Button("Copy Library Path", action: copyPath)
    }
}

You can create the menu’s title with a LocalizedStringKey, as seen in the previous example, or with a view builder that creates multiple views, such as an image and a text view:


Menu {
    Button("Open in Preview", action: openInPreview)
    Button("Save as PDF", action: saveAsPDF)
} label: {
    Label("PDF", systemImage: "doc.fill")
}

Primary Action

Menus can be created with a custom primary action. The primary action will be performed when the user taps or clicks on the body of the control, and the menu presentation will happen on a secondary gesture, such as on long press or on click of the menu indicator. The following example creates a menu that adds bookmarks, with advanced options that are presented in a menu.


Menu {
    Button(action: addCurrentTabToReadingList) {
        Label("Add to Reading List", systemImage: "eyeglasses")
    }
    Button(action: bookmarkAll) {
        Label("Add Bookmarks for All Tabs", systemImage: "book")
    }
    Button(action: show) {
        Label("Show All Bookmarks", systemImage: "books.vertical")
    }
} label: {
    Label("Add Bookmark", systemImage: "book")
} primaryAction: {
    addBookmark()
}

Styling Menus

Use the menuStyle(_:) modifier to change the style of all menus in a view. The following example shows how to apply a custom style:


Menu("Editing") {
    Button("Set In Point", action: setInPoint)
    Button("Set Out Point", action: setOutPoint)
}
.menuStyle(EditingControlsMenuStyle())

Topics 話題

Creating a Menu from Content

Creating a Menu with a Primary Action

Creating a Menu from a Configuration

Styling a Menu あるメニューをスタイル指定する

Supporting Types 支援を行う型

Default Implementations 省略時実装

Relationships 関係

Conforms To 次に準拠

See Also 参照

Buttons ボタン