我已经创建了UIButton方法。现在我想为动作按钮设置角落半径。我如何添加这个?

问题描述:

此代码不正确。我如何重写这段代码?或以其他方式解决这个问题。我已经创建了UIButton方法。现在我想为动作按钮设置角落半径。我如何添加这个?

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

设置

目标C

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

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

夫特

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; 

    } 
+0

检查更新的答案 –

+0

谢谢。这是行得通的,但是当我点击这个按钮时,只会发生。 –

+0

哦,简单的,然后删除视图做负载线和添加行中的按钮操作 –

定义这个IBAction为在哪里?如果您尝试为按钮设置角落半径,那么您可能需要将该发件人转换为UIButton *,然后设置该UIButton实例层的cornerRadius。

虽然我不会推荐这个解决方案。如果你可以发布你正在尝试解决的所有问题,我将能够更好地回答它。

通过在下面的方法,你的按钮:

目标C:

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

} 

这样称呼它:

[self addRoundedCorner:actionBtn Radius:4]; 

斯威夫特

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

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