Adding a custom view to toolbar

user531461 picture user531461 · Feb 15, 2011 · Viewed 7.4k times · Source

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.

Answer

user531461 picture user531461 · Feb 15, 2011

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.