how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

前端 未结 10 1175
Happy的楠姐
Happy的楠姐 2020-12-04 06:49

Is there a way to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?

I tried the following, but it ended up making the view disappea

10条回答
  •  借酒劲吻你
    2020-12-04 07:15

    Use a custom UIView and Implement the below code

    #import "CustomUIView.h"
    
    @implementation CustomView
    
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        CAShapeLayer * maskLayer = [CAShapeLayer layer];
        maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){20.0, 20.0}].CGPath;
    
        self.layer.mask = maskLayer;
    }
    
    @end
    

    Output:

    enter image description here

    Look at the UIBezierPath.h options :

    typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
        UIRectCornerTopLeft     = 1 << 0,
        UIRectCornerTopRight    = 1 << 1,
        UIRectCornerBottomLeft  = 1 << 2,
        UIRectCornerBottomRight = 1 << 3,
        UIRectCornerAllCorners  = ~0UL
    };
    

提交回复
热议问题