我已经创建了 UIButton 方法。现在我想为操作按钮设置角半径。我怎样才能添加这个?

I have created UIButton method. now I want set corner radius for action button. How can I add this?

此代码不正确。我如何重写这段代码?或任何其他解决此问题的方法。?

 - (IBAction)actionButton:(id)sender {
 }
 actionButton.layer.cornerRadius = 4.0f;

//my original code
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
backGroundImage.image = [UIImage imageNamed:@"3.png"];
   blurAlertAction.layer.cornerRadius = 4.0f;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)blurAlertAction:(UIButton*)sender {

sender.layer.cornerRadius = 4.0f;
BlurViewAlert * addView = [BlurViewAlert new];
[addView bluredView];
[addView setBlurEffectStyle:UIBlurEffectStyleDark];
[addView setTitle:@"Set your goal"];
[addView setPlaceholder:@"$"];
addView.onSubmit = ^BOOL(NSString *value){
    NSLog(@"%@",value);
    return YES;
 };

[addView show];
}

设置

Objective-C

- (IBAction)actionButton:(UIButton*)sender {
   sender.layer.cornerRadius = 4;

  // the above line is not working try below line 
  actionButton.layer.cornerRadius = 4.0f;
}

Swift

func actionButton(sender:UIButton!)
{
sender.layer.cornerRadius = 4

 // the above line is not working try below line 
  actionButton.layer.cornerRadius = 4
println("Button is working")
}

如果你想在外面这样做 UIButton 像这样操作

override func viewDidLoad() {

 super.viewDidLoad()

  // then don't write here 
  actionButton.layer.cornerRadius = 4

 }

选择

  - (void)viewDidLoad {
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
 backGroundImage.image = [UIImage imageNamed:@"3.png"];

}




- (IBAction)blurAlertAction:(UIButton*)sender {

 sender.layer.cornerRadius = 4.0f;

  }

这个IBAction是在哪里定义的?如果您尝试为点击时的按钮设置圆角半径,那么您可能希望将该发送器转换为 UIButton *,然后设置该 UIButton 实例层的 cornerRadius。

不过我不会推荐这个解决方案。如果你能post你试图解决的整个问题,我会更好地回答它。

通过以下方法传递您的按钮:

Objective c:

-(void)addRoundedCorner:(UIButton*)viewToRound Radius:(CGFloat)radius{
    CALayer* layer = viewToRound.layer;
    layer.cornerRadius=radius;
    layer.masksToBounds = true;

}

这样称呼它:

[self addRoundedCorner:actionBtn Radius:4];

Swift:

func addRoundedCorner(viewToRound:UIButton, radius:CGFloat) {

    var layer:CALayer = viewToRound.layer;
    layer.cornerRadius=radius
    layer.masksToBounds = true
} //F.E.