Dino Angelov Software Architect

Showing and hiding alternate menu items in Swift on macOS

• 1 minute read

For v2 of Rest I wanted to have menu items that show alternate titles and actions when the user holds Option.

After going through a couple of answers on StackOverflow and discussions they linked to, I found out it’s now way easier than ever to achieve it. There’s no need to run custom loops or set unnecessary keyboard shortcuts at all.

So here’s how you do it.

First, in your Storyboard/XIB, create all your menu items. Every second menu item should be set as alternate. For example, if you’ve created the following menu items:

Screenshot showing Xcode with a menu created

Then, select all your alternate items, and:

  1. Mark them as Alternate
  2. Set ⌥ (Option key) as a Key Equivalent.

Screenshot showing Xcode alternate items and key equivalent

They also need to be connected to the same action.

Screenshot showing Xcode with actions connected

And that’s it. Here it is in action.

Video size: 175kb