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

后端 未结 26 3287
佛祖请我去吃肉
佛祖请我去吃肉 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:51

    Swift 4 Swift 5 easy way in 1 line

    Usage:

    //MARK:- Corner Radius of only two side of UIViews
    self.roundCorners(view: yourview, corners: [.bottomLeft, .topRight], radius: 12.0)
    

    Function:

    //MARK:- Corner Radius of only two side of UIViews
    func roundCorners(view :UIView, corners: UIRectCorner, radius: CGFloat){
            let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            view.layer.mask = mask
    }
    

    In Objective-C

    Usage:

    [self.verticalSeparatorView roundCorners:UIRectCornerTopLeft radius:10.0];

    Function used in a Category (only one corner):

    -(void)roundCorners: (UIRectCorner) corners radius:(CGFloat)radius {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
            CAShapeLayer *mask = [[CAShapeLayer alloc] init];
            mask.path = path.CGPath;
            self.layer.mask = mask;
        }
    

提交回复
热议问题