NSButton in NSToolbarItem (setView) when you click "Text only" forces the mode "Icon and shortcut"

I am trying to recreate beautiful textured buttons, such as Finder, Safari and Transmission, on the toolbar. First, I started by simply dragging the Texture button to IB, etc. Everything works well, unless the user sets the toolbar to Text Only mode. When he presses the button, the icon and shortcut will be displayed on the toolbar. I remove the alles code and delegates from the toolbar to make sure this is not a problem with the code.

Then, to make sure I created a new project (no code at all), and I can reproduce the problem with a pure NSWindow with NSToolbar with one NSToolbarItem with NSButton in it.

Adding NSButtons with code:

- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar { return [NSArray arrayWithObject:@"myToolbarMenu"]; } - (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar { return [self toolbarAllowedItemIdentifiers:toolbar]; } - (NSToolbarItem*)toolbar:(NSToolbar*)toolbar itemForItemIdentifier:(NSString*)str willBeInsertedIntoToolbar:(BOOL)flag { if ([str isEqualToString:@"myToolbarItem"] == YES) { NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:str]; [item setView:[[NSButton alloc] init]]; [item setMinSize:NSMakeSize(50,50)]; [item setMaxSize:NSMakeSize(50,50)]; [item setLabel:@"Text"]; return [item autorelease]; } return nil; } 

But it also has the same effect: when I click NSToolbarItem with NSButton in it in the "Text Only" mode, the toolbar itself makes it the "Icon and Text" mode.

Do you have any idea how I can make it work correctly, or maybe there is an alternative to creating beautiful looking toolbars like Safari etc.?

+7
cocoa interface-builder nstoolbar
source share
1 answer

You need to add a menu view for each NSToolbarItem that has a custom view. Below the line where you highlight NSToolbarItem, add the following:

 NSMenuItem *menuRep = [[NSMenuItem alloc] initWithTitle:@"Text" action:@selector(targetMethod:) keyEquivalent:@""]; [menuRep setTarget:<target>]; [item setMenuFormRepresentation:menuRep]; 

As long as the goal is valid, your elements should remain in the form of text buttons; otherwise they will be disabled. See Customizing the presentation of a toolbar position .

Normally, you also need to implement validateToolbarItem: for your purpose, but for custom view elements, you instead need to override the check: to do something suitable. See Testing Toolbar Items .

+8
source share

All Articles