Provide quick access to useful actions by adding menus and keyboard shortcuts to your Mac app built with Mac Catalyst.
- iOS 13.0+
- Xcode 11.4+Beta
- Mac Catalyst 13.0+
This sample project demonstrates how to add menu commands and keyboard shortcuts to the menu bar. The sample app uses its
Menu object to insert a
UIMenu object that adds the following menus:
New — Appears at the beginning of the File menu and contains two operations: Date Item and Text Item.
Navigation — Contains a group of
UIKeyobjects for command-key navigation.
Style — Contains a group of
UICommandobjects that have checkmark states. This grouping looks like a font-style menu for text formatting.
Tools — Contains a group of
The sample project also shows you how to add contextual menus to views, and how to handle menu-command selection using
Add Menus to the Menu Bar
The Mac version of your iPad app comes with a standard menu bar. You use the
UIMenu class, an object representing the main or contextual menu system, to modify the menu bar.
This sample also uses
UIMenu to add menus to the
main system menu by implementing
App. This function receives a
UIMenu object that the sample then uses to add and remove menus. Because menus can exist with no window or view hierarchy, the system only consults
UIApplication to build the app’s menu bar.
Menu commands consist of
UIAction objects that are grouped in a
Add a Menu Command to the File Menu
This sample inserts a
UIKey called Command-O into the File Menu and creates a corresponding keyboard shortcut:
Notice that the
UIKey title is a localized string using the
NSLocalized function, which can display the menu name in multiple languages.
This sample inserts the Open command into the middle of the menu bar’s File menu:
Contribute to the Edit Menu
Editing operations, such as cut, copy, paste, and delete, are commonly used in most apps. In this sample app, you can access these operations through the Edit menu, where you can edit the sample’s left-side content or its master table-view content. These operations represent the first-responder functions:
cut(_ sender: Any?),
copy(_ sender: Any?),
paste(_ sender: Any?),
delete(_ sender: Any?).
Add Commands to Control the User Interface
In this sample, you can change the primary or left-side table view’s selection by using
UIKey. These key commands are connected to the up and down arrow keys and are added directly to the table view. The following example shows how to add the down arrow key as a
This sample also demonstrates how to add menu commands as command-key equivalents. The following example shows how to create a menu with all four arrow keys as Command-keys:
Display Contextual Menus
This sample contains two kinds of contextual menus. One uses
UIMenu, a menu interface positioned relative to a target rectangle or view. The other uses
UIContext, an interaction object used to display relevant actions for your content.
For the custom view (
Responsive), whose background color is determined by user preference, a long press or a tap-and-hold gesture displays a
UIMenu for editing the master table view items. Use
can to determine which actions appear in that contextual menu.
For the other contextual menu, the user control-clicks or right-mouse clicks, or does a tap and hold on the rest of the
Detail content. The sample uses
UIContext to display Copy, Rename, and Share commands. This kind of contextual menu is a grouping of
UIAction is a menu element that performs its action in a closure. In iOS, you optionally customize this contextual menu’s highlight preview by using
context. This delegate function returns a
UITargeted, the source view used when opening and animating the contextual menu.
Adjust Menu Commands
This sample creates its Edit menu content based on the first responder in the responder chain. You can adjust menu commands by using
can. The master view controller overrides this function to enable menu commands based on the table view state or the state of the pasteboard.
To change other menu commands, this sample implements
validate, where you can adjust those specific commands.
Add a Preferences Menu
Mac apps typically display app-specific preferences using a preferences window. This sample adds a preferences window by adding a Settings bundle to the Xcode project’s target. The window automatically becomes available to the user through the Preferences menu command in the Application menu. To learn more, see Displaying a Preferences Window.