以编程方式设置RoundedRect按钮

时间:2014-07-13 09:58:37

标签: ios objective-c ios7 uibutton

我尝试使用以下内容设置圆形按钮,但它从视图中消失。如果我删除了

takebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

然后显示矩形按钮。帮我拿到RoundedRect按钮。

 takebtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 325, 250, 40)];
    takebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [takebtn setTitle:@"Take Photo" forState:UIControlStateNormal];
    takebtn.backgroundColor=[UIColor colorWithRed:50.0/255.0 green:205.0/255.0 blue:50.0/255.0 alpha:1.0];
    [takebtn addTarget:self
                action:@selector(takePhoto:)
      forControlEvents:(UIControlEvents)UIControlEventTouchDown];
    [nextview addSubview:takebtn];

2 个答案:

答案 0 :(得分:1)

您需要修改以下代码: -

takeBtn.layer.cornerRadius = 10; //value can be change accordingly.
takeBtn.clipsToBounds = YES;

注意: - 无需在ios7中导入Quartz框架

答案 1 :(得分:1)

试试这个

UIButton* takebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takebtn setFrame:CGRectMake(30, 325, 250, 40)];
[takebtn setTitle:@"Take Photo" forState:UIControlStateNormal];
takebtn.backgroundColor=[UIColor colorWithRed:50.0/255.0 green:205.0/255.0 blue:50.0/255.0 alpha:1.0];
[takebtn addTarget:self
            action:@selector(takePhoto:)
  forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[nextview addSubview:takebtn];