0

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 
1
  • Dont use frame. Add button.translatesAutoresizingMaskIntoConstraints = false before adding constraints Commented Mar 11, 2019 at 13:16

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer! :) I choose it best. By the way this answer is good one stackoverflow.com/a/32087869/11058551

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.