As soon as we've a square-size frame, we can easily do that using the
cornerRadius provided in CALayer.
// #import <QuartzCore/QuartzCore.h> view.layer.cornerRadius = CGRectGetWidth(view.frame)/2; view.layer.maskToBounds = YES;
By setting the
cornerRadius exactly half of the length of the dimensions, we get a perfect circular masked shape.
The beauty of this is the fact that it's a property on CALayer, we can even configure this in Interface Builder with zero lines of code:
layer.cornerRadiusto User Runtime Attributes
Make sure Clip Subviews is checked
And there you'll get the same result in runtime. :) It's that simple trick I learnt from here.