Dashed line border around UIView

Sohaib picture Sohaib · Dec 3, 2012 · Viewed 94.4k times · Source

How do I add dashed line border around UIView.

Something Like this

Answer

Chris picture Chris · Mar 7, 2013

Another method if you like sublayers. In your custom view's init, put this (_border is an ivar):

_border = [CAShapeLayer layer];
_border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = @[@4, @2];
[self.layer addSublayer:_border];

And in your layoutsubviews, put this:

_border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
_border.frame = self.bounds;