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

Upvotes: 0

Views: 1895

Answers (1)

Carsten
Carsten

Reputation: 1081

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

Upvotes: 1

Related Questions