How can I hide an NSToolbarItem using code?
Asked Answered
G

4

7

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?

Gaikwar answered 10/2, 2011 at 22:2 Comment(0)
B
13

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.

Braunstein answered 10/2, 2011 at 22:7 Comment(0)
S
1

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

Squish answered 10/2, 2011 at 22:8 Comment(1)
And how do I re-add them? I don't see a "addItemToToolbar" method. The closest thing i could find is "- (void)insertItemWithItemIdentifier:(NSString *)itemIdentifier atIndex:(NSInteger)index". is that the method I am looking for? Will I need to implement anything else on my delegate?Antonomasia
K
0

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.

Koziol answered 2/3 at 14:39 Comment(0)
B
0

It's now finally possible with macOS 15 - the NSToolbarItem class now has a hidden property: https://developer.apple.com/documentation/appkit/nstoolbaritem/4357352-hidden

Bacteriolysis answered 22/9 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.