Round some corners of UIView and round the view’s layer’s border too

前端 未结 3 1325
鱼传尺愫
鱼传尺愫 2020-12-25 15:09

I am trying to round the bottom two corners of a UIView, and have the layer’s border show up rounded as well. I am currently doing:

UIRectCorners corners = U         


        
3条回答
  •  天命终不由人
    2020-12-25 15:52

    Zev Eisenberg's answer is the right one.

    Although I prefer:

    [self.layer addSublayer:strokeLayer];
    

    instead of creating and adding a subview:

    CGSize radii = CGSizeMake(radius, radius);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                               byRoundingCorners:corners
                                                     cornerRadii:radii];
    
    // Mask the container view’s layer to round the corners.
    CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
    [cornerMaskLayer setPath:path.CGPath];
    self.layer.mask = cornerMaskLayer;
    
    // Make a transparent, stroked layer which will dispay the stroke.
    CAShapeLayer *strokeLayer = [CAShapeLayer layer];
    strokeLayer.path = path.CGPath;
    strokeLayer.fillColor = [UIColor clearColor].CGColor;
    strokeLayer.strokeColor = color.CGColor;
    strokeLayer.lineWidth = 2; // the stroke splits the width evenly inside and outside,
    // but the outside part will be clipped by the containerView’s mask.
    
    [self.layer addSublayer:strokeLayer];
    

提交回复
热议问题