subclassing nscontrol

I'm trying to develop my own NSControl.The reasons of this are very complex, but to resume, I want my own controls reacting with a web server, in many applications, without rewrite the same libraries, over and over.

But if my reasons are complex, I think my question is not so complex. The control is very simple: it needs to draw a view with white background and a defined height.


My code is:


import Cocoa

@IBDesignable class myTextField: NSControl {
@IBInspectable var height: CGFloat {
  get{
  return frame.size.height
  }
  set (aValue) {

  let aSize = CGSize(width: self.bounds.size.width, height: aValue)
  setFrameSize(aSize)
  }
}


override init(frame frameRect: NSRect) {
  super.init(frame: frameRect)
  height = 30
  _init()
}

required init?(coder: NSCoder) {
  super.init(coder: coder)
  needsLayout = true
  height = 30
  _init()
}

func _init() {
  let aSize = CGSize(width: self.bounds.size.width, height: height)
  setBoundsSize(aSize)
  setFrameSize(aSize)
}

override func prepareForInterfaceBuilder() {
  _init()
  super.prepareForInterfaceBuilder()
  needsDisplay = true
  let aSize = CGSize(width: self.bounds.size.width, height: height)
  setFrameSize(aSize)
}


override func draw(_ dirtyRect: NSRect) {
  Swift.print("bounds \(self.bounds)")
  Swift.print("Frame \(self.frame)")
  var newRect = dirtyRect
  newRect.size.height = height
  super.draw(newRect)

  NSColor.white.setFill()
  newRect.fill()
}



If I put a customView on my storyboard this view is designed properly

if I subclass the custom view as being a myTextfield, the background is transparent and whatever the value of the height property, the design doesn't change.


When I build and run my project, the myTextfield show properly.


What did I missed?

I have noticed that IBInspectable property should not be a computed var. That has to deal with the time when the evaluation is done by IB.


You could probably use prepareForInterfaceBuilder to set the values (func is used only by IB)


    override func prepareForInterfaceBuilder() {

        super.prepareForInterfaceBuilder()
          // Do any setup for IB
    }
subclassing nscontrol
 
 
Q