I want to edit border width and background color of an NSView
and made those values IBInspectable
:
@IBInspectable var borderWidth: CGFloat{
set{
layer?.borderWidth = newValue
}
get{
return layer!.borderWidth
}
}
@IBInspectable var backgroundColor: NSColor{
set{
layer?.backgroundColor = newValue.CGColor
}
get{
return NSColor(CGColor: layer!.backgroundColor)!
}
}
In the init
method, I wrote:
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
wantsLayer = true
layer?.setNeedsDisplay()
}
When I run the app, changes are shown correctly, but the view doesn't live-render in the interface builder. I don't get any errors or warnings either.
i found the solution myself: it seems that the init Method does not get called when the view gets rendered in the Interface builder. As a solution i had to add a global variable which creates a
CALayer()
when needed:and now it finally renders in the interface builder as well.