如果有约束,如何更改 UIButton 的高度和宽度?这是给你的简单问题。但我不明白。这是我更改大小的代码的一部分
button.frame = CGRect(x: button.frame.origin.x, y: button.frame.origin.y, width: 30.0, height: 30.0)
button.widthAnchor.constraint(equalToConstant: 30.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
最佳答案
如果您有约束,则需要存储对每个约束的引用并更新常量,如下所示:
let button = UIButton()
let widthConstraint = button.widthAnchor.constraint(equalToConstant: 30.0)
let heightConstraint = button.heightAnchor.constraint(equalToConstant: 30.0)
NSLayoutConstraint.activate([widthConstraint, heightConstraint])
//change button size to 50x50
widthConstraint.constant = 50
heightConstraint.constant = 50
最好, 卡斯滕
关于ios - 如何改变 Swift 中按钮的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55102023/