如何设置RoundedRectagle的cornerRadius只有UIButton的左下角,右下角?
问题描述:
我试图设置按钮底部的圆角矩形。但他们只适用于底部的权利。如何设置RoundedRectagle的cornerRadius只有UIButton的左下角,右下角?
我希望双方的按钮底部有圆角矩形。 这是我的代码:
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];
输出:
答
试试这个
CAShapeLayer * maskLayer1 = [CAShapeLayer layer];
maskLayer1.path = [UIBezierPath bezierPathWithRoundedRect: registerbtn.bounds byRoundingCorners: UIRectCornerBottomRight | UIRectCornerBottomLeft cornerRadii: (CGSize){9.0, 9.0}].CGPath;
registerbtn.layer.mask = maskLayer1; registerbtn.layer.cornerRadius=9; // this value vary as per your desire
registerbtn.clipsToBounds = YES;
答
这是工作的罚款。试试吧
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];
答
你的代码看起来不错。
只要写相同的代码在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];
}
+0
是的,它的工作感谢 –
+0
太棒了!HTH @ user7306261 –
答
上面提到的答案应该工作,但问题是你的按钮正从两侧圆形的,但其效果是不可见的给你。如果你通过缩放来检查,那么你可以看到你的按钮是有角度的,然后在上面的图层下面有另一个层,你在其上添加了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
这应该可以工作,但您需要小心应用该图层。
它不工作...相同的输出将显示 –
只是试试这个代码。 https://github.com/joprithiviraj/ButtonCornerRadius –
@bhavin拉马尼,你的回答是正确的,但他只问右侧角半径。所以不想使用UIRectCornerBottomLeft。谢谢.... –