Rounding the corners of an UIView

Rounded corners are all the rage at the moment, so you would think that there would be an API on UIView to do such a thing. If you looked at all of the UIView documentation, you would come to the conclusion that there is not an API to do such a thing, and you would be (kind of) wrong.

Although UIView does not allow you round its corners, CALayer does, and every UIView is backed by a CALayer.

To access all of the functionality of CALayer, you will need to import QuartzCore into any of header/implementation files that you intend to use CALayer in.

#import <QuartzCore/QuartzCore.h>

CALayer has a lot of APIs available to it, but the property we are interested in is:

@property CGFloat cornerRadius

As the property suggests, this allows you to set the CALayer‘s corner radius in points:

UIView *view = [[UIView alloc] initWithFrame:frame];
view.layer.cornerRadius = 10.0;

CALayer also has properties allowing you to set its border, shadow etc, so all of these can save you a lot of code … and unwanted images.