How to set cornerRadius for only top-left and top-right corner of a UIView?

后端 未结 26 2995
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:14

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

I tried the following, but it end up not seeing the

26条回答
  •  灰色年华
    2020-11-22 06:52

    Here is a short method implemented like this:

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *openInMaps = [UIButton new];
        [openInMaps setFrame:CGRectMake(15, 135, 114, 70)];
        openInMaps = (UIButton *)[self roundCornersOnView:openInMaps onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:NO radius:5.0];
    }
    
    - (UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius {
    
        if (tl || tr || bl || br) {
            UIRectCorner corner = 0;
            if (tl) {corner = corner | UIRectCornerTopLeft;}
            if (tr) {corner = corner | UIRectCornerTopRight;}
            if (bl) {corner = corner | UIRectCornerBottomLeft;}
            if (br) {corner = corner | UIRectCornerBottomRight;}
    
            UIView *roundedView = view;
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
            CAShapeLayer *maskLayer = [CAShapeLayer layer];
            maskLayer.frame = roundedView.bounds;
            maskLayer.path = maskPath.CGPath;
            roundedView.layer.mask = maskLayer;
            return roundedView;
        }
        return view;
    }
    

提交回复
热议问题