class RoundImage: UIImageView {
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
...
func setupView() {
self
When this code gets run the view hasn't been sized yet, so the width is incorrect. Try putting a listener on frame:
override var frame : CGRect {
didSet {
layer.cornerRadius = frame.size.width / 2
}
}
I faced with this problem a few days ago.
You should add layoutIfNeeded()
before layer modifications.
In your case:
func setupView() {
self.layoutIfNeeded()
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor
self.clipsToBounds = clip
self.layer.cornerRadius = self.frame.size.width / 2
}