2016-12-16 12 views
1

ボタンの丸い四角形のボタンを設定しようとしました。しかし彼らは底の右だけを適用する。UIButtonの左下、右下のみRoundRectagleのcornerRadiusを設定する方法は?

ボタンの両側に丸みのある矩形があります。 は、これは私のコードです:

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

答えて

0

はちょうどこのコードを試してみてください。この

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

それが動作していない...同じ出力が表示 –

+0

になりますしてみてください。 https://github.com/joprithiviraj/ButtonCornerRadius –

+0

@bhavin Ramani、あなたの答えは正しいですが、彼は右角の半径だけを聞いています。だから、UIRectCornerBottomLeftを使いたくありません。ありがとう.... –

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]; 
} 
+0

ええ、その働きのおかげで –

+0

素晴らしい!.. HTH @ user7306261 –

0

を動作するはずですが、問題は、あなたのボタンであるが、両側から丸めなっているが、その効果はあなたには見えません。ズームして確認すると、ボタンがぎこちないように見えて、上のレイヤーの下にベジェパスを追加した別のレイヤーがあります。

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 

これは機能するはずですが、レイヤーを適用する際には注意が必要です。

関連する問題