Class

NSMenu

An NSMenu object manages an application’s menus.

Symbols

Managing the Menu Bar

class func menuBarVisible()

Returns a Boolean value that indicates whether the menu bar is visible.

class func setMenuBarVisible(Bool)

Sets whether the menu bar is visible and selectable by the user.

var menuBarHeight: CGFloat

The menu bar height for the main menu in pixels.

Creating an NSMenu Object

init(title: String)

Initializes and returns a menu having the specified title and with autoenabling of menu items turned on.

Adding and Removing Menu Items

func insertItem(NSMenuItem, at: Int)

Inserts a menu item into the menu at a specific location.

func insertItem(withTitle: String, action: Selector?, keyEquivalent: String, at: Int)

Creates and adds a menu item at a specified location in the menu.

func addItem(NSMenuItem)

Adds a menu item to the end of the menu.

func addItem(withTitle: String, action: Selector?, keyEquivalent: String)

Creates a new menu item and adds it to the end of the menu.

func removeItem(NSMenuItem)

Removes a menu item from the menu.

func removeItem(at: Int)

Removes the menu item at a specified location in the menu.

func itemChanged(NSMenuItem)

Invoked when a menu item is modified visually (for example, its title changes).

func removeAllItems()

Removes all the menu items in the menu.

Finding Menu Items

func item(withTag: Int)

Returns the first menu item in the menu with the specified tag.

func item(withTitle: String)

Returns the first menu item in the menu with a specified title.

func item(at: Int)

Returns the menu item at a specific location of the menu.

var numberOfItems: Int

The number of menu items in the menu, including separator items.

var items: [NSMenuItem]

An array containing the menu items in the menu.

Finding Indices of Menu Items

func index(of: NSMenuItem)

Returns the index identifying the location of a specified menu item in the menu.

func indexOfItem(withTitle: String)

Returns the index of the first menu item in the menu that has a specified title.

func indexOfItem(withTag: Int)

Returns the index of the first menu item in the menu identified by a tag.

func indexOfItem(withTarget: Any?, andAction: Selector?)

Returns the index of the first menu item in the menu that has a specified action and target.

func indexOfItem(withRepresentedObject: Any)

Returns the index of the first menu item in the menu that has a given represented object.

func indexOfItem(withSubmenu: NSMenu?)

Returns the index of the menu item in the menu with the given submenu.

Managing Submenus

func setSubmenu(NSMenu?, for: NSMenuItem)

Assigns a menu to be a submenu of the menu controlled by a given menu item.

func submenuAction(Any?)

The action method assigned to menu items that open submenus.

var supermenu: NSMenu?

The parent menu that contains the menu as a submenu.

var isTornOff: Bool

Indicates whether the menu is offscreen or attached to another menu (or if it’s the main menu).

Deprecated

Enabling and Disabling Menu Items

var autoenablesItems: Bool

Indicates whether the menu automatically enables and disables its menu items.

func update()

Enables or disables the menu items of the menu based on the NSMenuValidation informal protocol and sizes the menu to fit its current menu items if necessary.

Getting and Setting the Menu Font

var font: NSFont!

The font of the menu and its submenus.

Handling Keyboard Equivalents

func performKeyEquivalent(with: NSEvent)

Performs the action for the menu item that corresponds to the given key equivalent.

Simulating Mouse Clicks

func performActionForItem(at: Int)

Causes the application to send the action message of a specified menu item to its target.

Managing the Title

var title: String

The title of the menu.

Configuring Menu Size

var minimumWidth: CGFloat

The minimum width of the menu in screen coordinates.

var size: NSSize

The size of the menu in screen coordinates

Getting Menu Properties

var propertiesToUpdate: NSMenuProperties

The available properties for the menu.

Managing Menu Change Notifications

var menuChangedMessagesEnabled: Bool

Indicates whether messages are sent to the application’s windows each time the menu changes.

Deprecated

Displaying Contextual Menus

var allowsContextMenuPlugIns: Bool

Indicates whether the pop-up menu allows appending of contextual menu plug-in items.

Displaying Context-Sensitive Help

class func popUpContextMenu(NSMenu, with: NSEvent, for: NSView)

Displays a contextual menu over a view for an event.

class func popUpContextMenu(NSMenu, with: NSEvent, for: NSView, with: NSFont?)

Displays a contextual menu over a view for an event using a specified font.

func helpRequested(with: NSEvent)

Overridden by subclasses to implement specialized context-sensitive help behavior.

Deprecated
func popUp(positioning: NSMenuItem?, at: NSPoint, in: NSView?)

Pops up the menu at the specified location.

Managing Display of the State Column

var showsStateColumn: Bool

Indicates whether the menu displays the state column.

Controlling Allocation Zones

class func menuZone()

Returns the zone from which NSMenu objects should be allocated.

Deprecated

Handling Highlighting

var highlightedItem: NSMenuItem?

Indicates the currently highlighted item in the menu.

Managing the User Interface

var userInterfaceLayoutDirection: NSUserInterfaceLayoutDirection

Configures the layout direction of menu items in the menu.

Managing the Delegate

var delegate: NSMenuDelegate?

The delegate of the menu.

Handling Tracking

func cancelTracking()

Dismisses the menu and ends all menu tracking.

func cancelTrackingWithoutAnimation()

Dismisses the menu and ends all menu tracking without displaying the associated animation.

Constants

NSMenuProperties

These constants are used as a bitmask for specifying a set of menu or menu item properties, and are contained by the propertiesToUpdate property.

Notifications

static let NSMenuDidAddItem: NSNotification.Name

Posted after a menu item is added to the menu.

static let NSMenuDidChangeItem: NSNotification.Name

Posted after a menu item in the menu changes appearance.

static let NSMenuDidEndTracking: NSNotification.Name

Posted when menu tracking ends, even if no action is sent.

static let NSMenuDidRemoveItem: NSNotification.Name

Posted after a menu item is removed from the menu.

static let NSMenuDidSendAction: NSNotification.Name

Posted just after the application dispatches a menu item’s action method to the menu item’s target.

static let NSMenuWillSendAction: NSNotification.Name

Posted just before the application dispatches a menu item’s action method to the menu item’s target.