【问题标题】:how to set cornerRadius for RoundedRectagle only bottom-left,bottom-right of a UIButton?如何仅在 UIButton 的左下角、右下角为 RoundedRectagle 设置cornerRadius?
【发布时间】:2016-12-16 11:04:01
【问题描述】:

我尝试设置按钮底部圆角矩形。但它们仅适用于底部右侧。

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

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];

输出:

【问题讨论】:

    标签: ios objective-c uibutton uibezierpath


    【解决方案1】:

    这工作正常。试试看吧

    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】:

    您的代码看起来不错。

    只需在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];
    }
    

    【讨论】:

    • 太棒了!..HTH@user7306261
    【解决方案3】:

    上述答案应该有效,但问题是您的按钮从两侧变圆,但您看不到它的效果。如果您将通过缩放进行检查,那么您可以看到您的按钮是拐角的,然后在上面添加了 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
    

    这应该可以,但您需要小心应用图层。

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 2015-09-22
      • 2012-04-27
      • 2012-09-13
      • 2016-06-08
      • 2013-12-22
      • 2011-04-14
      • 1970-01-01
      相关资源
      最近更新 更多