如何为ROUNDRectagle设置cornerRadius只在UIButton的左下角,右下角?

时间:2016-12-16 11:04:02

标签: ios objective-c uibutton uibezierpath

我尝试设置按钮底部的圆角矩形。但他们只适用于最低权利。

我希望按钮的两侧底部都有圆角矩形。 这是我的代码:

UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:registerbtn.bounds
                                                    byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                                         cornerRadii:CGSizeMake(9.0, 9.0)];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = registerbtn.bounds;
shapeLayer.path = shapePath.CGPath;    
[registerbtn.layer addSublayer:shapeLayer];

输出:

enter image description here

3 个答案:

答案 0 :(得分:0)

这很好用。试一试

UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:registerbtn.bounds
                                                byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                      cornerRadii:CGSizeMake(9.0, 9.0)];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = registerbtn.bounds;
shapeLayer.path = shapePath.CGPath;

[registerbtn.layer addSublayer:shapeLayer];

答案 1 :(得分:0)

您的代码看起来很好。

只需在viewWillAppear中编写相同的代码并检查:

-(void)viewWillAppear:(BOOL)animated{

     [super viewWillAppear:animated];

     UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:registerbtn.bounds
                                                    byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                                         cornerRadii:CGSizeMake(9.0, 9.0)];

     CAShapeLayer *shapeLayer = [CAShapeLayer layer];
     shapeLayer.frame = registerbtn.bounds;
     shapeLayer.path = shapePath.CGPath;

     [registerbtn.layer addSublayer:shapeLayer];
}

答案 2 :(得分:0)

上面提到的答案应该有效,但问题是你的按钮从两侧变圆,但它的效果对你来说是不可见的。如果您将通过缩放进行检查,那么您可以看到您的按钮已被逼入,然后在上面的图层下面添加了另一个图层,您已添加了bezierpath。

let shapePath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: [UIRectCorner.BottomLeft,UIRectCorner.BottomRight], cornerRadii: CGSizeMake(8,8)) 
let newCornerLayer = CAShapeLayer()
newCornerLayer.frame = view.bounds
newCornerLayer.path = shapePath.CGPath;
view.layer.mask = newCornerLayer

这应该可行,但您需要注意应用图层。