【发布时间】:2018-09-07 10:59:59
【问题描述】:
也许我的眼睛在欺骗我,但是在使用此代码为类似于 iPhone 的电话应用程序的数字键盘创建圆形按钮之后:
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
【问题讨论】:
-
你展示了你的其余代码吗?
-
就是这样。我在 Storyboard 中添加了按钮,并使用此代码修改形状。我从一个旧的 Objective-C 项目中改编了代码,原始代码产生了完美的圆圈。你能告诉我你对哪个代码感兴趣吗?
-
好的,当您打印框架时会得到什么?
-
让我检查一下...
-
请参阅上面的编辑框和边界大小...
标签: ios button rounded-corners