【问题标题】:I have created UIButton method. now I want set corner radius for action button. How can I add this?我创建了 UIButton 方法。现在我想为动作按钮设置角半径。我怎样才能添加这个?
【发布时间】:2025-11-20 18:10:02
【问题描述】:

此代码不正确。我怎样才能重写这段代码?或任何其他方式来解决这个问题。?

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

【问题讨论】:

    标签: ios objective-c iphone ios7


    【解决方案1】:

    设置

    Objective-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;
    
      }
    

    【讨论】:

    • 谢谢。这是有效的,但是只有当我点击这个按钮时,它才会发生。
    • 哦,简单,然后删除视图中确实加载的行并在按钮操作中添加该行
    • 它是否有效,如果您需要帮助我修改我的答案
    • 你能发布你更新的代码吗,在这里你需要你选择哪个按钮的圆角半径,更正或者你需要其他任何东西
    • @user3352317 : uibutton 的 iboutlet 的名称是什么?
    【解决方案2】:

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

    不过,我不会推荐此解决方案。如果您可以发布您要解决的整个问题,我将能够更好地回答它。

    【讨论】:

      【解决方案3】:

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

      目标 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.
      

      【讨论】:

        最近更新 更多