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

In my project I have a NSToolBar which has 5 NSToolBarItems . I want to remove last two toolbarItems in which one is NSToolbarSpaceItemIdentifier , and reload the toolbar again.

How to do this? I am stuck at this point for quiet a long time. If I want to remove the items from NSToolBar , I am getting constraint warning due to NSToolbarSpaceItemIdentifier .

I want to recall the delegate method once again and pass the latest items array to it.

How to do this?
Please help.

I could not find a way to refresh. However Jay is correct and his advice can be used to quickly reset the menus

//Factory Method for reseting toolbar
func setCurrentToolBarItems(desiredItems:[String]){
    // remove all toolbar items
    for _ in (window?.toolbar?.items)!{
        window?.toolbar?.removeItem(at: 0)
    // add new items
    for item in desiredItems{
        window?.toolbar?.insertItem(withItemIdentifier: item, at: 0)

Here is some additional code to create buttons programmatically.

import Cocoa
class ToolbarWindowController: NSWindowController, NSToolbarDelegate {
    @IBOutlet var DualToolBar: NSToolbar!
    enum ToolbarItemID : String {
        case DeleteCard = "DeleteSelectedCard", RandomizeCards = "RandomizeCards", CardTest = "CardTest",SwitchFirstCardFaceSeen = "SwitchFirstFace", ExitTest = "ExitTest"
        static let allValues = [DeleteCard, RandomizeCards, CardTest, SwitchFirstCardFaceSeen, ExitTest]
    override func windowDidLoad() {
        super.windowDidLoad()
        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    func test() {
        contentViewController?.performSegue(withIdentifier: "goToSlideShow", sender: contentViewController)
        setCurrentToolBarItems(desiredItems: [ToolbarItemID.RandomizeCards.rawValue, ToolbarItemID.SwitchFirstCardFaceSeen.rawValue])
    //Factory Method for reseting toolbar
    func setCurrentToolBarItems(desiredItems:[String]){
        // remove all toolbar items
        for _ in (window?.toolbar?.items)!{
            window?.toolbar?.removeItem(at: 0)
        // add new items
        for item in desiredItems{
            window?.toolbar?.insertItem(withItemIdentifier: item, at: 0)
    // MARK: - NSToolbarDelegate
    func customToolbarItem(itemForItemIdentifier itemIdentifier: String, label: String, paletteLabel: String, toolTip: String, target: AnyObject, itemImage: NSImage, action: Selector?) -> NSToolbarItem? {
        let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)
        toolbarItem.label = label
        toolbarItem.paletteLabel = paletteLabel
        toolbarItem.toolTip = toolTip
        toolbarItem.target = target
        toolbarItem.action = action
        toolbarItem.image = itemImage
        return toolbarItem
     NSToolbar delegates require this function.
     It takes an identifier, and returns the matching NSToolbarItem. It also takes a parameter telling
     whether this toolbar item is going into an actual toolbar, or whether it's going to be displayed
     in a customization palette.
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: String, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        var toolbarItem: NSToolbarItem = NSToolbarItem()
        /* We create a new NSToolbarItem, and then go through the process of setting up its attributes from the master toolbar item matching that identifier in our dictionary of items.
        switch itemIdentifier {
        case ToolbarItemID.CardTest.rawValue:
            let image = NSImage(named: "NSSlideshowTemplate")!
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.CardTest.rawValue, label: "test", paletteLabel: "test", toolTip: "test yourself with this cardset", target: self, itemImage: image, action: #selector(self.test))!
        case ToolbarItemID.RandomizeCards.rawValue:
            let image = NSImage(named: "Randomize Button")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.RandomizeCards.rawValue, label: "randomize", paletteLabel: "randomize", toolTip: "randomize cards so that you are not always tested with the cards in the same order", target: self, itemImage: image!, action: nil)!
        case ToolbarItemID.DeleteCard.rawValue:
            let image = NSImage(named: "deleteButton")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.DeleteCard.rawValue, label: "delete", paletteLabel: "delete", toolTip: "delete card on current selected row", target: self, itemImage: image!, action: nil)!
        case ToolbarItemID.SwitchFirstCardFaceSeen.rawValue:
            let image = NSImage(named: "Switch")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.SwitchFirstCardFaceSeen.rawValue, label: "switch def/term", paletteLabel: "switch def/term", toolTip: "Allows users to test themselves using either the definition or the term first", target: self, itemImage: image!, action: nil)!
        case ToolbarItemID.ExitTest.rawValue:
            let image = NSImage(named: "Return to editing 2")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.ExitTest.rawValue, label: "editor window", paletteLabel: "editor window", toolTip: "Used to exit testing and reenter editing mode", target: self, itemImage: image!, action: nil)!
        default:
            Swift.print("more buttons must be added")
        return toolbarItem
     NSToolbar delegates require this function.  It returns an array holding identifiers for the default
     set of toolbar items.  It can also be called by the customization palette to display the default toolbar.
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [String] {
        if contentViewController is CardViewer{
            return [ToolbarItemID.CardTest.rawValue]
        }else{
            return [ToolbarItemID.RandomizeCards.rawValue]
        /*  Note:
         That since our toolbar is defined from Interface Builder, an additional separator and customize
         toolbar items will be automatically added to the "default" list of items.

I think this link is the most complete for the topic of tool bars. I found the sample code especially helpful. "toolbar programming topics for cocoa": https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Toolbars/Toolbars.html

The constraint warning appears to be a separate issue - We have seen this bug under 10.8 but not under 10.9 for the same project.
As far as I can tell this is a frameworks bug and not related to any kind of wrong usage of the APIs.

As far as removing toolbar items is concerned:
removeItemAtIndex: is what you're looking for.

As usual, the docs are your friend. In this case please have a read here
Toolbar Programming Topics for Cocoa

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.