Some of the frames of my views can be set only after layoutSubviews
has been called:
class CustomButton: UIButton {
let border = CAShapeLayer()
init() {
super.init(frame: CGRectZero)
border.fillColor = UIColor.whiteColor().CGColor
layer.insertSublayer(border, atIndex: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
border.frame = layer.bounds
border.path = UIBezierPath(rect: bounds).CGPath
}
}
Because I want to animate border
in a later state, I only want the code in layoutSubviews
to be called once. To do that, I use the following code:
var initial = true
override func layoutSubviews() {
super.layoutSubviews()
if initial {
border.frame = layer.bounds
border.path = UIBezierPath(rect: bounds).CGPath
initial = false
}
}
Question:
I was wondering if there is a better way of doing this. A more elegant and functional way, without having to use an extra variable.