I am currently working on a custom themed NSButton
. Every tutorial, or guide I have found requires to subclass NSButtonCell
, even the guide from Ap
I would definitely spend more time looking into the layer-backed view
approach. I'm not sure why it didn't work for you because there's no reason for layers not to work on an NSButton
(effectively an NSView
derivative).
Also weighing on looking more into layers is your mentioning of animation.
Some code extracted from a project I am working on (custom NSButton
):
From init()...
self.wantsLayer = true
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay
self.layer?.borderColor = NSColor.gridColor().CGColor
self.layer?.borderWidth = 0.5
self.layer?.backgroundColor = NSColor.whiteColor().CGColor
You can then get fine-grained control in the display cycle specific to layers with:
override var wantsUpdateLayer:Bool{
return true
}
override func updateLayer() {
// your code here
super.updateLayer()
}
If your custom button needs a shape then you can even use a CAShapeLayer below to make your backing layer a special shape...more detail to be looked into.
override func makeBackingLayer() -> CALayer {
return CAShapeLayer()
}