Search code examples
cocoaappkit

How can I create a named section in an NSPopUpButton


I'd like to duplicate the appearance of the menu below, with named "sections" in the pop-up button's menu.

NSPopUpButton with named sections

I can tell that the grey horizontal lines are NSMenuItem.separator() but setting the title and attributed title on those doesn't work. Adding submenus gets me nowhere, as does adding disabled menu items. This seems to be a common pattern, and I find it hard to believe that it's just ad-hoc with attributed strings / custom views everywhere.


Solution

  • What you need is a NSMenuItem.separator() plus a disabled NSMenuItem to display the title. With NSMenuItem attributedTitle and indentationLevel you can customise the appearance of the text. Consider that NSMenu autoenablesItems can also have an effect.

    Best, Marc