0

UIButton corners only two sides i.e. top right and bottom right with following:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_nearmeButton.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(20.0, 20.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path  = maskPath.CGPath;
_nearmeButton.layer.mask = maskLayer;

But how to give border colour to above UIButton same as given image enter image description here.

LGP
  • 4,135
  • 1
  • 22
  • 34
Minkle Garg
  • 723
  • 3
  • 9
  • 35

1 Answers1

1

You are on the right track. Instead of setting your path as a mask you can add it to the buttons layer. Basically (more or less) every UIView is backed by a CALayer. Just set the colors you want on the stroke and the fill and add it to the buttons layer, and you are done.

UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:_nearmeButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)];

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path  = borderPath.CGPath;
borderLayer.strokeColor = [UIColor redColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
borderLayer.lineWidth = 1.0;
[_nearmeButton.layer addSublayer:borderLayer];

If your buttons size changes you need to update the layer size also.

LGP
  • 4,135
  • 1
  • 22
  • 34
  • This is in the case, if we want the corners on 4 sides, but I need corner only on 2 sides and border on all sides. – Minkle Garg Feb 06 '18 at 18:20