iosinterface-buildernslayoutconstraintibdesignable

Is there a way to make a custom constraint work in interface builder?


Make a UIView with a fixed height constraint, notice I set it to say 40 in IB.

Change the constraint class to this:

@IBDesignable class BadassHeightConstraint: NSLayoutConstraint {

    override var constant: CGFloat {
        set {
            super.constant = newValue
        }
        get {
            return calc()
        }
    }

    private func calc() -> CGFloat {
        return 100.0
    }
}

This works perfectly at runtime, left image, it's 100, not 40.

But, I cannot get it to work in IB. Why is this so?

enter image description here

Is there any solution to have these work in IB?


Solution

  • Putting it simply, only a UIView can be @IBDesignable.