CGFloat dashPattern[]= {3.0, 2};
context =UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// And draw with a blue fill color
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 4.0);
CGContextSetLineDash(context, 0.0, dashPattern, 2);
CGContextAddRect(context, self.bounds);
// Close the path
CGContextClosePath(context);
CGContextStrokePath(context);
// Fill & stroke the path
CGContextDrawPath(context, kCGPathFillStroke);
Swift solution with custom class worked with autolayout
customized from @Iain Smith
For this you need add CAShapeLayer for that particular object
In swift 4 I created an UIView extension with the following function:
Use CGContextSetLineDash() method.
I think it will be helpful to you.
I ended up creating a IB Designable using some of @Chris implementation:
CurvedDashedBorderUIVIew.h:
CurvedDashedBorderUIVIew.m:
then just set it up in the xib/storyboard:
For those of you working in Swift, this class extension on UIView makes it easy. This was based on sunshineDev's answer.
To use it: