Ian Gordon
Ian Gordon

Reputation: 51

How do I use isAlternative with an NSMenuItem with a customView?

I'm attempting to use the isAlternative property with an NSMenuItem with a custom view. It is not behaving as a standard NSMenuItem without a custom view. How do I get the standard modifier-key toggling behavior when using NSMenuItems with custom views?

Standard Menu Item behavior

Add a menu item. Add an additional menu item with .isAlternate == true and .keyEquivalentModifierMask = [ .option ] (or another appropriate modifier key).

When the menu is displayed, the first menu item is displayed. While the modifier key is held, the original menu item is replaced with the alternate menu item.

Custom Menu Item behavior

Add a menu item with a custom view. Add an additional menu item with a custom view with .isAlternate == true and .keyEquivalentModifierMask = [ .option ] (or another appropriate modifier key).

When the menu is display, instead of the standard toggling behavior, both the original and the alternate menu items are shown at all times.

Reference

GitHub example project

Upvotes: 3

Views: 603

Answers (1)

ixany
ixany

Reputation: 6070

How to set up an alternate NSMenuItem

  1. Set isAlternate to true
  2. Define a keyEquivalentModifierMask
let alternativeMenuItem = NSMenuItem("alternative", action: nil, keyEquivalent: "")
alternativeMenuItem.isAlternate = true
alternativeMenuItem.keyEquivalentModifierMask = [.option]

Upvotes: 2

Related Questions