这些纽扣是圆形的还是结节状的?
Are these buttons round, or nodular?
也许我的眼睛在欺骗我,但在使用此代码为类似于 iPhone 的 Phone 应用程序的数字键盘创建圆形按钮后:
for subStack in (mainStackView?.arrangedSubviews)! {
for thisView in (subStack.subviews) {
if let button = thisView as? UIButton {
button.layer.borderWidth = 2
button.layer.borderColor = orangeBorderColor
button.frame.size.width = 76
button.frame.size.height = 76
button.layer.cornerRadius = 0.5 * (button.bounds.size.height)
button.layer.masksToBounds = true
// button.clipsToBounds = true
button.backgroundColor = UIColor.lightGray
button.titleLabel?.font = button.titleLabel?.font.withSize(30)
button.setTitleColor(.black, for: .normal)
}
}
}
我得到的东西看起来像这样:
我试过使用 clipsToBounds
、maskToBounds
,我试过按钮的 .frame
及其 .bounds
属性 作为基础算术,但它们在我看来仍然是结节状而不是圆形。
有人可以提供平滑它们的建议吗?
谢谢!
编辑
当我添加这个时:
print(button.frame.size as Any)
print(button.bounds.size as Any)
print(button.layer.cornerRadius as Any)
我在控制台中得到这个:
(76.0, 76.0)
(76.0, 76.0)
38.0
不能直接设置使用约束布局的视图的框架,堆栈视图的排列子视图必须使用约束布局。那是你的问题。自动布局(稍后)会覆盖您设置框架的尝试,因此您的角半径与按钮的大小不匹配。
创建 UIButton
的子类。在您的子类中,覆盖 layoutSubviews
以设置按钮的 cornerRadius
(并且不要忘记调用 super.layoutSubviews
)。
也许我的眼睛在欺骗我,但在使用此代码为类似于 iPhone 的 Phone 应用程序的数字键盘创建圆形按钮后:
for subStack in (mainStackView?.arrangedSubviews)! {
for thisView in (subStack.subviews) {
if let button = thisView as? UIButton {
button.layer.borderWidth = 2
button.layer.borderColor = orangeBorderColor
button.frame.size.width = 76
button.frame.size.height = 76
button.layer.cornerRadius = 0.5 * (button.bounds.size.height)
button.layer.masksToBounds = true
// button.clipsToBounds = true
button.backgroundColor = UIColor.lightGray
button.titleLabel?.font = button.titleLabel?.font.withSize(30)
button.setTitleColor(.black, for: .normal)
}
}
}
我得到的东西看起来像这样:
我试过使用 clipsToBounds
、maskToBounds
,我试过按钮的 .frame
及其 .bounds
属性 作为基础算术,但它们在我看来仍然是结节状而不是圆形。
有人可以提供平滑它们的建议吗?
谢谢!
编辑
当我添加这个时:
print(button.frame.size as Any)
print(button.bounds.size as Any)
print(button.layer.cornerRadius as Any)
我在控制台中得到这个:
(76.0, 76.0)
(76.0, 76.0)
38.0
不能直接设置使用约束布局的视图的框架,堆栈视图的排列子视图必须使用约束布局。那是你的问题。自动布局(稍后)会覆盖您设置框架的尝试,因此您的角半径与按钮的大小不匹配。
创建 UIButton
的子类。在您的子类中,覆盖 layoutSubviews
以设置按钮的 cornerRadius
(并且不要忘记调用 super.layoutSubviews
)。