Search code examples
iosswiftautolayoutaspect-ratio

1:1 width:height constraint to UIButton programmatically


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


Solution

  • Try this

    myButton.addConstraint(NSLayoutConstraint(item: myButton, attribute: .height, relatedBy: .equal, toItem: myButton, attribute: .width, multiplier: 1, constant: 0))