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


@interface UIMenu : UIMenuElement


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 the user 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 the user selects an element from the menu, the system executes the code that you provide.

Listing 1

Adding a menu group containing 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 menu item at the top of the File menu.
builder.insertChild(newMenu, atStartOfMenu: .file)

To see examples of how to use UIMenu, see Adding Menus and Shortcuts to the Menu Bar and User Interface.


Creating a Menu Object

+ menuWithTitle:children:

Creates a menu with the specified title and children menu elements.

+ menuWithTitle:image:identifier:options:children:

Creates a new menu with the specified values.


Constants for identifying an app's standard menus.


Options for configuring a menu's appearance.

Accessing the Child Elements


The contents of the menu.

- menuByReplacingChildren:

Creates a new menu with the same configuration as the current menu, but with a new set of child elements.

Getting the Menu Details


The unique identifier for the current menu.


The configuration options for the current menu.


Inherits From

See Also

App Menus


An interface for adding and removing menus from a menu system.


An object representing a main or contextual menu system.