ADC Home > Reference Library > Technical Q&As > Cocoa > User Experience >

How to get custom views to show up in NSToolbarItems


Q: I have created an NSToolbarItem and added a custom view using "-setView:". My custom view never shows up in my NSToolbar, however, even after adding the NSToolbarItem and installing the NSToolbar in my window. My standard NSToolbarItems (that contain images) show up just fine. What am I doing wrong here?

A: NSToolbarItems have a default minimum/maximum width/height of 0,0. Calling "-setImage:" on an NSToolbarItem to give it an image changes the size to be the size of the image, but calling "-setView:" to instead give it a custom view does not. Thus, your custom view never is visible on the screen, because it has a minimum/maximum width and height of 0. Call "-setMinSize:" and "-setMaxSize:" on your NSToolbarItem to set the proper sizes when creating it, and your custom view will become visible.



 [newNSToolbarItem setMinSize:[customView bounds].size];
[newNSToolbarItem setMaxSize:[customView bounds].size];
Listing 1. An example of setting the NSToolbarItem size




[May 03 2001]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.