14

I'm struggling with Cocoa for 2 hours now without success. I want to add a custom view to the toolbar. So, I added a NSToolbar to the window (with IB), and added my view (which works perfectly). IB automatically created a NSToolbarItem. I followed the instructions from Apple here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Toolbars/Tasks/AddRemoveToolbarItems.html#//apple_ref/doc/uid/20000755-BBCGJCDJ

The problem is that I don't know what to do now, the view doesn't show although it's label is displayed in the window.

Here's the code I use to draw (very simple, it's for testing purpose)

- (void)drawRect:(NSRect)dirtyRect {
    [[NSColor blackColor] set];
    [[NSBezierPath bezierPathWithRect:self.bounds] fill];
}

Can someone help me?

Thanks in advance.

  • You'll probably need to post the code you're using to create and maintain the view. The code in the documentation worked for me when copy/pasted it and adapted it for my own app a few years back, so unless you're using it 100% as-is, it's likely a problem with your custom code. :-) – Joshua Nozzi Feb 15 '11 at 19:17
  • I updated my post. I guess there's nothing more to write since I use IB. Am I right ? –  Feb 15 '11 at 19:36

2 Answers2

19

I solved the problem.

I put my custom view in the root of the nib. I added a classic NSToolbarItem and created two outlets: one for the custom view and one for the NSToolbarItem. On -(void)awakeFromNib, I called setView: on the NSToolbarItem with the custom view.

According to some ressources on the internet, it is a bug with Interface Builder.

  • Thanks, indeed there is some kind of bug in Xcode, and it's still an issue in version 4.2. – charles Mar 20 '12 at 05:46
  • 5
    When you drag a NSView to a toolbar in IB it automatically creates a NSToolbarItem holding that view, you already had a NSToolbarItem all you needed was to programmatically call setView on the NSToolbarItem as opposed to just setting your custom view in IB (probably a bug with the way xibs are unpacked) – valexa Dec 01 '12 at 11:08
7

According to an Apple engineer in the discussion at http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg35673.html, there is a bug in Interface Builder whereby "Custom Views" (NSViews created in IB) are not decoded properly when used as the view for an NSToolbarItem, and so do not appear in the toolbar. Other kinds of NSViews, such as NSButtons and NSBoxes, will work just fine as toolbar items: you can create these in Interface Builder and then drag them into the toolbar to make them into toolbar items.

(The discussion in the link above implies that the bug is down to how "Custom Views" are created from the XIB at runtime: using initWithFrame: instead of initWithCoder:. The discussion dates from 2009 but this still hasn't been fixed as of XCode 4.5/OS X 10.8.)

In my case I was using a regular NSView to wrap a set of controls (a volume slider and min/max buttons), rather than implementing a custom NSView subclass. I was able to avoid the problem by using an NSBox as the container instead of an NSView: I made the NSBox transparent, title-less and borderless, so it otherwise acted exactly like a plain NSView wrapper. This was a little more work in IB, but saved me the trouble of wiring up the view to the toolbar item programmatically.

Alun Bestor
  • 186
  • 1
  • 5