Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I can't seem to get toolbar items to show up in my programatic Swift app, they don't appear from what I can tell. I build a standard NSWindow, and then attempt the following. I've managed to get File Menus and Status bar menus to work without needing any nibs/xibs/delegates and I'd like to accomplish the same with toolbars.

let toolbar = NSToolbar()
let itemId = NSToolbarItem.Identifier("Identifier")
var toolbarItem: NSToolbarItem
toolbarItem = NSToolbarItem(itemIdentifier: itemId)
toolbarItem.label = String("Label")
toolbarItem.title = String("Title")
toolbarItem.toolTip = String("help")
toolbarItem.isEnabled = true
toolbar.insertItem(withItemIdentifier: itemId, at: toolbar.items.count)
toolbar.isVisible = true
toolbar.displayMode = .iconAndLabel
window.toolbar = toolbar
window.toolbar?.displayMode = .iconAndLabel

Below we can see the toolbar exist, however it doesn't shows any toolbar items.enter image description here

Adding code from the toolbar Delegate to help:

class ToolbarDelegate: NSToolbar, NSToolbarDelegate {
var toolbarAllowedItemIdentifiersList: [NSToolbarItem.Identifier] = [.print, .space, .showFonts, .showColors, .toggleSidebar, .cloudSharing, .flexibleSpace, .showColors];
var toolbarDefaultItemIdentifiersList: [NSToolbarItem.Identifier] = [.print, .space, .showFonts, .showColors, .toggleSidebar, .cloudSharing, .flexibleSpace, .showColors];
func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
    return nil
func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
    return toolbarAllowedItemIdentifiersList
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
    return toolbarDefaultItemIdentifiersList
                Does this answer your question? NSToolbar created programmatically starts empty and won't save
– Willeke
                Oct 23, 2020 at 7:29
                @Willeke Your suggestion answer one part of my problem. Setting the delegate first did allow me to finally see the built in toolbar items. But it didn't help with my own custom items. I have since figured that out though.
– Joshua Briefman
                Oct 23, 2020 at 22:31

Willeke's commment helped me with displaying system default toolbar options which weren't previously visible, but I had another problem that prevented my own custom toolbar options from displaying.

The example code I was following here macOS Menubar and Toolbar without storyboard or .xib menu bar in Swift 5 - Ryan Theodore The had nil'd out func toolbar in the toolbar delegate. So any attempt to add after instantiation was ignored. Second, when you add items to the toolbar, you do it by identifier. Problem is, the identifier doesn't have any info on the toolbar item itself. So I needed to provide that information for every identifier I needed to add.

This left me with something like as follows. I created two dictionaries, one to store the string name to identifier reference, and a second which contains an identifier to toolbaritem reference.

Then below that I call to add the new toolbar item to the list, and then I insert the identifier of the item in the toolbar (where the toolbar function) then fetches the toolbar item using the identifier and returns it to be added to the toolbar.

class ToolbarDelegate: NSToolbar, NSToolbarDelegate {
    var definedToolbarStringIdentifiers: [String:NSToolbarItem.Identifier] = [:]
    var definedToolbarIdentifierItems: [NSToolbarItem.Identifier:NSToolbarItem] = [:]
    func addDefinedToolbarItem(toolBarString: String, toolbarItem: NSToolbarItem) {
        if (!definedToolbarStringIdentifiers.keys.contains(toolBarString)) {
            definedToolbarStringIdentifiers[toolBarString] = toolbarItem.itemIdentifier
            definedToolbarIdentifierItems[toolbarItem.itemIdentifier] = toolbarItem
    var toolbarAllowedItemIdentifiersList: [NSToolbarItem.Identifier] = [.print, .space, .showFonts, .showColors, .toggleSidebar, .cloudSharing, .flexibleSpace, .showColors];
    var toolbarDefaultItemIdentifiersList: [NSToolbarItem.Identifier] = [.print, .space, .showFonts, .showColors, .toggleSidebar, .cloudSharing, .flexibleSpace, .showColors];
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        if (self.definedToolbarIdentifierItems.keys.contains(itemIdentifier)) {
            return self.definedToolbarIdentifierItems[itemIdentifier]
        } else {
            return nil
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return toolbarAllowedItemIdentifiersList
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return toolbarDefaultItemIdentifiersList

Then later on I call the following:

var toolbarItemB: NSToolbarItem
let itemIdB = NSToolbarItem.Identifier(rawValue: "Testing")
toolbarItemB = NSToolbarItem(itemIdentifier: itemIdB)
toolbarItemB.label = String("Hello Label")
//toolbarItemB.title = String("Hello Title")
toolbarItemB.toolTip = String("Hello help")
toolbar.addDefinedToolbarItem(toolBarString: "Testing", toolbarItem: toolbarItemB)
toolbar.insertItem(withItemIdentifier: itemIdB, at: toolbar.items.count)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.