需要帮助将此按钮变成圆形

时间:2016-01-06 22:57:28

标签: ios swift

我们有一个矩形框,它是一个按钮 - 内部圆圈是一个图像。由于约束(stackViews)

,按钮被拉伸

按钮的宽度为197,高度为77

enter image description here

我们尝试将其转换为圆形,但它不起作用 - 我们使用此代码

// Mehtod 1
    circleButton.layer.cornerRadius = 0.5 * circleButton.bounds.size.width

    // Method 2
    circleButton.layer.cornerRadius = (circleButton.imageView?.bounds.size.width)! * 0.5

我们正试图在运行时将其变成一个圆圈。因此它可以与圆形图像大小相同。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"TimoonPumba.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];

//width and height should be same value
button.frame = CGRectMake(0, 0, ROUND_BUTTON_WIDTH_HEIGHT, ROUND_BUTTON_WIDTH_HEIGHT);

//Clip/Clear the other pieces whichever outside the rounded corner
button.clipsToBounds = YES;

//half of the width
button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;

[self.view addSubview:button];

相关问题