Unfortunately you cannot do this using cornerRadius
and autolayout — the CGLayer
is not affected by autolayout, so any change in the size of the view will not change the radius which has been set once causing, as you have noticed, the circle to lose its shape.
You can create a custom subclass of UIImageView
and override layoutSubviews
in order to set the cornerRadius
each time the bounds of the imageview change.
EDIT
An example might look something like this:
class Foo: UIImageView {
override func layoutSubviews() {
super.layoutSubviews()
let radius: CGFloat = self.bounds.size.width / 2.0
self.layer.cornerRadius = radius
}
}
And obviously you would have to constrain the Foobar
instance’s width to be the same as the height (to maintain a circle). You would probably also want to set the Foobar
instance’s contentMode
to UIViewContentMode.ScaleAspectFill
so that it knows how to draw the image (this means that the image is likely to be cropped).