// Put this code in ur drawRect
Goal - C
- (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x,y, width, height) cornerRadius:6].CGPath; CGContextAddPath(ctx, clippath); CGContextSetFillColorWithColor(ctx, self.color.CGColor); CGContextClosePath(ctx); CGContextFillPath(ctx); [self.color set]; [_path closePath];
Swift 3
func drawRect(rect : CGRect) { // Size of rounded rectangle let rectWidth = rect.width let rectHeight = rect.height // Find center of actual frame to set rectangle in middle let xf:CGFloat = (self.frame.width - rectWidth) / 2 let yf:CGFloat = (self.frame.height - rectHeight) / 2 let ctx: CGContext = UIGraphicsGetCurrentContext()! ctx.saveGState() let rect = CGRect(x: xf, y: yf, width: rectWidth, height: rectHeight) let clipPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: rectCornerRadius).cgPath ctx.addPath(clipPath) ctx.setFillColor(rectBgColor.cgColor) ctx.closePath() ctx.fillPath() ctx.restoreGState() }
source share