How to draw a simple rounded rect in swift (rounded corners)

mcfly soft picture mcfly soft · May 21, 2015 · Viewed 37.1k times · Source

I managed to draw a rect :-) But I don't know how to draw a rounded rect.

Can someone help me out with the following code how to round the rect?

let canvas = UIGraphicsGetCurrentContext()
rec = CGRectMake(0, 0, 40, 40);

//var maskPath = UIBezierPath(roundedRect: rec, byRoundingCorners: .BottomLeft | .BottomRight, cornerRadii: CGSize(width: 3, height: 3))

CGContextAddRect(canvas, rec);
CGContextFillPath(canvas);

Answer

Icaro picture Icaro · May 21, 2015

How to create a rounded corner rectangle using BezierPath

 var roundRect = UIBezierPath(roundedRect: <CGRect>, byRoundingCorners: <UIRectCorner>, cornerRadii: <CGSize>)

Or for an example with values:

var roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 100, 100), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(16.f, 16.f))