How to create a rounded UIButton with a radius of only the left and bottom left corners

I need to create a button with an upper left and lower left corner radius like this enter image description here. I tried to create the following extension, which was taken from one of stackoverflow's answers:

extension UIButton {

   func roundCorners(corners:UIRectCorner, radius: CGFloat) {
       self.layer.borderColor = GenerateShape.UIColorFromHex(0x989898, alpha: (1.0-0.3)).CGColor
       self.layer.borderWidth = 1.0
       let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
       let mask = CAShapeLayer()
       mask.path = path.CGPath
       self.layer.mask = mask

   }
}

then the method is called as follows:

self.collectionBtn.roundCorners(.TopLeft | .BottomLeft, radius: cornerRadius) 

this code generates the following form enter image description here

So why are the upper left and lower left angles invisible? What to do to make them visible?

+4
source share
2 answers

. - , , . , "" . , .

, , . .

:

extension UIButton 
{
  func roundCorners(corners:UIRectCorner, radius: CGFloat) 
  {
    let borderLayer = CAShapeLayer()
    borderLayer.frame = self.layer.bounds
    borderLayer.strokeColor = GenerateShape.UIColorFromHex(0x989898, 
      alpha: (1.0-0.3)).CGColor
    borderLayer.fillColor = UIColor.clearColor().CGColor
    borderLayer.lineWidth = 1.0
    let path = UIBezierPath(roundedRect: self.bounds, 
      byRoundingCorners: corners, 
      cornerRadii: CGSize(width: radius, height: radius))
    borderLayer.path = path.CGPath
    self.layer.addSublayer(borderLayer);
  }
}

EDIT:

Swift 2:

self.collectionBtn.roundCorners(.TopLeft | .BottomLeft, radius: 10)

self.collectionBtn.roundCorners([.TopLeft, .BottomLeft], radius: 10)
+7

:

CGRect maskRect = CGRectMake(0.0, 0.0, CGRectGetWidth(<#something here#>), CGRectGetHeight(<#something here#>));
CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:maskRect
                                           byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerTopLeft)
                                                 cornerRadii:CGSizeMake(<#corner radius#>, <#corner radius#>)];
maskLayer.path = path.CGPath;
self.layer.mask = maskLayer;
0

Source: https://habr.com/ru/post/1610197/


All Articles