简体   繁体   中英

How to change size of button in Swift?

How to change height and width of UIButton, if it's constraint? This is for you easy question. But I don't understand. This is part of code where I change size

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

If you have constraints you need to store a reference to each constraint and update the constant like this:

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

Best, Carsten

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM