Does Interface Builder's new IBDesignable attribute work with NSButton and other controls in Cocoa?

1.5k views Asked by At

I have managed to get the IBDesignable/IBInspectable attributes working with direct subclasses of NSView but not with a direct subclass of NSButton. This is causing me to question if in fact the Cocoa implementation is somehow limited to NSView only.

Almost every example on the web (and Apple WWDC 2014 Xcode video) use NSView and then drag a custom view component from the library onto the canvas (and then change its class).

Is it possible to use IBDesignable with subclasses of NSControl and NSButton etc...? I have seen many examples on the web using UIButton.

If it is possible, then what are you supposed to drag from the library onto the canvas? It doesn't make sense for it to be a "custom view". On the other hand, there is no "custom control" available.

To be clear, I can get the IBInspectable attribute to show up at design time; but any changes don't seem to live render at design time.

The workaround is to wrap any custom NSButton I want to create within an NSView (via composition) but this seems like a bit of a hack...

2

There are 2 answers

4
catlan On

I started playing around with a custom NSButton and NSButtonCell.

Dragging a button from the library onto the canvas and changing its class and the cell class doesn't live render. I think this is because Interface Builder still does a lot of custom things to setup NSButtonCell.

What works fine for me is dragging a custom view from the library onto the canvas and set its class. For this to work you need to setup the cell inside NSButtons -initWithCoder:.

Also I found a sample from Apple with a layer-backed custom Checkbox.

0
Wizard of Kneup On

You need to drag an NSButton onto the view, then set the Custom Class to your specific NSButton descendant. Not sure why it doesn't work when you start with an NSView.

What can give you a hint is that the NSButton specific attributes aren't in the "Attributes Inspector". Hence there must be some setup at the time you drag the control onto the view.