较小尺寸的按钮不圆 layer.cornerRadius

Button at smaller size not round with layer.cornerRadius

我尝试创建一个按钮并使用该代码设置样式:


    button.tintColor = UIColor.white
    button.contentEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    button.backgroundColor = .clear
    button.layer.cornerRadius = 50
    button.layer.borderWidth = 2
    button.layer.borderColor = UIColor.white.cgColor

我的按钮大小是 100x100,如果我把它变小(例如 80x80,如下所示),边框就不再是圆的了:

如何让中心圆也变圆?

使用下面的代码

button.layer.cornerRadius = button.frame.size.width/2

确保高度和宽度每次都相等。

请将角半径设置为buttonWidth/2。我假设您的按钮具有相同的宽度和高度。

button.layer.cornerRadius = button.frame.size.width/2