Question

I like to try to completely take over the area where the NSToolbar resides so I can put my own custom controls, views and background. The advantages of using this area are:

  • Any sliding panels appear below the toolbar area instead of just the title bar.
  • In Lion, the toolbar area comes down along with the menu bar when the mouse is at the top of the screen.

I have tried using a borderless window, and implementing my own custom views within it but unfortunately I lose the above advantages as well as having a few other minor problems.

My current method is to use the undocumented method '_toolbarView' with the NSToolbar and add my custom view into its subviews. This works fine as I can turn off toolbar customisation. Unfortunately, the size of the toolbar is initialised with the items within that toolbar. Does anyone know if I can change the size of toolbar without adding a fake ToolbarItem?

Maybe there's also a better way of doing this that I am currently unaware of. Thanks for any suggestions and comments.

Was it helpful?

Solution

No need to use any undocumented APIs. Just create a toolbar item with a custom view:

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
    NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
    …
    [item setView:myCustomToolbarView];
    …
}

You can control your custom toolbar’s size using the item’s minSize and maxSize properties (e. g. in your NSWindowDelegate’s -windowDidResize:).

Remember to also update the toolbar display mode so it doesn't show item labels:

[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top