【发布时间】:2019-11-22 00:57:54
【问题描述】:
我正在尝试以编程方式编写一个圆形按钮,但它总是以正方形结束。我已经尝试了几件事,到处看了看,但不知道还能做什么。 代码的第二部分在另一个类中。
static let warriorButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = .blue
button.setTitle("WARRIOR", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = true
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.isEnabled = true
return button
}()
warriorButton.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, paddingTop: 100, paddingLeft: 60, paddingBottom: 0, paddingRight: 0, width: 90, height: 90)
warriorButton.layer.cornerRadius = warriorButton.layer.bounds.size.width/2;
warriorButton.clipsToBounds = true;
【问题讨论】:
-
尝试添加
warriorButton.layer.masksToBounds = true,你可能需要warriorButton.isOpaque = true -
显示
func anchor(top: ...代码 -
在
warriorButton.layer.cornerRadius之前添加warriorButton.layoutIfNeeded()
标签: swift xcode11 swift5 programmatically