How do I add items to toolbar in macOS?

Asked on 08/03/2024

1 search

To add items to a toolbar in macOS, you can use the NSToolbar class, which has been enhanced in macOS Sequoia to give you more control over display mode, displayed items, and item visibility. Here are some key points to consider:

  1. Display Mode Customization: You can offer a choice of style (icon only or with text labels) using the allowsDisplayModeCustomization property. This is enabled by default. Ensure your toolbar has an identifier so that AppKit can save the style preference, and make sure all your toolbar items have good labels.

  2. Item Identifiers: Use the new itemIdentifiers property to set a toolbar's item identifiers. This automatically handles minimal additions and removals for you. Note that if allowsUserCustomization is enabled, changing values here will override any customizations, so use this for dynamic, non-customizable toolbars.

  3. Item Visibility: You can conditionally hide and show toolbar items using the new isHidden property. Hidden items still appear in customization, allowing users to choose where they want them to appear when made visible.

For more detailed information, you can refer to the session What’s new in AppKit (15:21).

Relevant Sessions