I want to make an @IBInspectable for UIView that makes UIView as a circle:
@IBInspectable var circleBorder: Bool {
set {
layer.cornerRadius = layer.frame.size.width / 2
}
get {
return layer.cornerRadius > 0 ? true : false
}
}
The problem is that my @IBInspectable is called before constraints modifies its value and a wrong cornerRadius is being set. Is it possible to get the layer size after constraints here?
I don't think there is, but there's a better way - expose cornerRadius
.
This way the developer can do things in their own sequence. Exposing things in a Bool
means that in IB a developer needs to (1) create the view, (2) add the constraints, (3) update the frames, and then (4) set the Bool
. (Also, in Xcode 8, I'm guessing if you change the device in IB - even after getting the sequence correct - things may not properly update.
Unless you have a specific reason - which I can't come up with - to make this a Bool
, this is not only harmless, it is more customizable.
The code:
@IBInspectable public var cornerRadius:CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}