Last Updated: February 25, 2016
·
5.094K
· iphonehelpr

Applying Rounded Corners to a View in Objective-C

In some instances it may be necessary to apply some lovely rounded corners to a UIView or one of its subclasses when building an iOS app. In some instances it is ok to access the layer property of the view and call setCornerRadius.

If performance suffers then using bezierPaths and masks can be the solution.

-(void) setCornerRadius
{

// Create a bezier path with the required corners rounded

UIBezierPath *cornersPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds  byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(5, 5)];

//Create a new layer to use as a mask

CAShapeLayer *maskLayer = [CAShapeLayer layer];

// Set the path of the layer 

maskLayer.path = cornersPath.CGPath;
self.view.layer.mask = maskLayer;

}