How can I hide an NSToolbarItem using code?

4.1k views Asked by At

I have a toolbar with various options and some options should not be visible in some cases. I have already figured out a way to disable them in my application, using this callback:

  - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem

But I haven't figured out a way to really hide them when I need. Anyone has any ideas on how to do this or if it's really possible at all?

3

There are 3 answers

0
David On BEST ANSWER

If disabling them is not enough you could remove the items.

[myToolbar removeItemAtIndex:itemIndex];

Then when you need to put them back, just re-insert them:

[myToolbar insertItemWithItemIdentifier:itemIdentifier atIndex:itemIndex];

Look at Adding and Removing Toolbar Items in the Apple docs.

1
Seva Alekseyev On

No built-in visibility property for buttons. Remove it from the toolbar and re-add when needed.

0
Ely On

Instead of endless adding and removing toolbar items in a single NSToolbar object to adjust the toolbar for a certain context, it is usually better to implement multiple NSToolbar instances, all with their own configuration for a certain context, and assign the applicable configuration to the toolbar property of the NSWindow.