Contents

UIMenu

A container for grouping related menu elements in an app menu or contextual menu.

Declaration

@MainActor class UIMenu

Overview

Create UIMenu objects and use them to construct the menus and submenus your app displays. You provide menus for your app when it runs on macOS, and key command elements in those menus also appear in the discoverability HUD on iPad when someone presses the Command key. You also use menus to display contextual actions in response to specific interactions with one of your views. Every menu has a title, an optional image, and an optional set of child elements. When someone selects an element from the menu, the system executes the code you provide. The code sample below illustrates adding a menu group that contains two menu elements — New and Open — to the File menu.

// Ensure that the builder is modifying the menu bar system.
guard builder.system == UIMenuSystem.main else { return }

let newDocument = UIKeyCommand(title: "New",
                               action: #selector(newDocument(_:)),
                               input: "n",
                               modifierFlags: .command)

let openDocument = UIKeyCommand(title: "Open...",
                                action: #selector(openDocument(_:)),
                                input: "o",
                                modifierFlags: .command)

// Use the .displayInline option to avoid displaying the menu as a submenu,
// and to separate it from the other menu elements using a line separator.
let newMenu = UIMenu(title: "", options: .displayInline, children: [newDocument, openDocument])

// Insert the menu item at the top of the File menu.
builder.insertChild(newMenu, atStartOfMenu: .file)

For examples of how you use UIMenu, see Adding menus and shortcuts to the menu bar and user interface.

Topics

Creating a menu object

Accessing child elements

Accessing selected elements

Getting menu details

Specifying size of menu elements

Customizing menu display

See Also

App menus