Build a toolbar in your app’s window, with a corresponding Touch Bar, for quick access to app features.
- macOS 10.13+
- Xcode 11.0+
A Toolbar manages the space immediately below a window’s title bar and above your app’s custom content. Toolbars manage controls that apply to the content into a window.
Each item in the toolbar is represented by an
NSToolbar object, created by Interface Builder or in code. This sample shows you how to integrate a toolbar into an
Create the Toolbar
Add a toolbar to your window by dragging an
NSToolbar object from Xcode’s Library window onto your
NSWindow object. Then connect the toolbar object to the window controller and make it the
Assign Toolbar Identifiers
NSToolbar you create, you must assign a unique identifier of type
NSToolbar. AppKit has predefined, built-in toolbar item identifiers for cloud sharing, printing, and showing the font and color palette. All toolbar items are arranged within the toolbar in an order determined by the user, and the order is autosaved as part of the toolbar’s configuration.
This sample creates two identifiers: one for setting the font size and one for setting the font style of an
Create the Allowed Toolbar Items
toolbar as a required function to return an array of toolbar item identifiers for the toolbar, specifying the contents and the order of the items in the configuration palette.
Create the Default Toolbar Items
toolbar to provide the default set of items that initially appear in the toolbar.
Create a Toolbar Item from an Identifier
As the toolbar delegate, you are responsible for creating a toolbar item for each identifier you define. At the time that AppKit instructs you to create an
NSToolbar, you create one based on the identifier. Implement
toolbar(_: to return the two toolbar items: Font Style and Font Size.
Each toolbar receives its own distinct set of identifiers. If a toolbar item has a custom view, that view should be in place when the item is returned. Do not assume that the returned item is added as an active toolbar item. In fact, the toolbar may ask for items here to construct the customization palette (it makes copies of the returned items). If
true, the returned item is inserted, and eventually the
toolbar delegate is called.
Use a Custom View for a Toolbar Item
When you apply a custom view to an
NSToolbar, you place one or more controls together to represent that
NSToolbar. In this sample, the custom views included are the font-style segmented control and the font size control, consisting of a label, stepper, and edit field.
Add More Attributes to a Toolbar Item
Use the optional
toolbar delegate function. This function is called when a new item is about to be added to the toolbar. At this time, you add or change initial state information for toolbar items, particularly those that you don’t directly control, like
NSToolbar. In this sample, you add a tooltip string to the print toolbar item.
Customize the Toolbar
You implement the delegate function
toolbar to determine which toolbar items users place in the toolbar. Users customize the item ordering by either control-clicking the toolbar, or by choosing Customize Toolbar from the View menu. This option opens a customization alert, where the user changes the layout. Only items returned by the delegate function
toolbar appear in that alert.
Add Touch Bar Support
This sample implements the Touch Bar to work in conjunction with the toolbar. What you see in the window’s toolbar is what you see in the Touch Bar. This sample uses the Touch Bar to set the font style and size the same way as the toolbar.
make and create the
Test the Touch Bar on a MacBook Pro. If you’re unable to do so, however, you may use Xcode’s Touch Bar simulator by choosing Window > Show Touch Bar.
Customize the Touch Bar
The user customizes the Touch Bar by choosing Customize Touch Bar from the View menu. To add this menu item to the main menu, implement the following: