1

How can I set the aspect ratio constraint to a 1:1 ratio of width:height on a UIButton programmatically in swift? This image shows in Interface Builder what I want to achieve through code.

My requirements in Interface Builder

maxwell
  • 3,788
  • 6
  • 26
  • 40
j.krissa
  • 223
  • 5
  • 21

1 Answers1

8

Try this

myButton.addConstraint(NSLayoutConstraint(item: myButton, attribute: .height, relatedBy: .equal, toItem: myButton, attribute: .width, multiplier: 1, constant: 0))
maxwell
  • 3,788
  • 6
  • 26
  • 40