13

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...

Sam
  • 2,745
  • 3
  • 20
  • 42

2 Answers2

0

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.

catlan
  • 25,100
  • 8
  • 67
  • 78
  • 1
    Could you elaborate on "For this to work you need to setup the cell inside NSButtons -initWithCoder:" and why this works. Recent documentation from Apple says not to touch the buttonCell so I haven't paid much attention to "cell" topics. Thanks. – Sam Jan 23 '15 at 14:23
  • Does someone have this sample? I've got an error "Sorry, that page cannot be found." =( – Nuzhdin Vladimir Jan 05 '17 at 00:25
  • @catlan - the link is dead – Duck Sep 23 '17 at 04:45
  • link is dead, tried the source from Github - sampe doesn't work anymore (tried checkbox.swift and it doesn't render if added as nsbutton to storyboard). Those who look for solution - it only works with custom NSViews dragged into xib/storyboard. – Marek H Apr 19 '20 at 10:13
0

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.

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35