Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Width equals height" constraint in Interface Builder

I can't find a way to create a 'square' constraint, meaning 'width equals height' in Interface Builder. I guess it's possible to add such constraint programmatically. Is there something I can do in IB? Maybe I just don't see it? It seems trivial, yet I can't find it.

like image 344
DemoniacDeath Avatar asked Mar 01 '13 22:03

DemoniacDeath


2 Answers

Update Xcode 5.1b5

width equals height

Ctrl+click and drag from a view and release while the pointer is over the view. Select "Aspect Ratio". It will create a constraint where the first and second item is the view.


Before Xcode 5.1

You can't because the width/height editor lacks the fields to relate to another property or set the ratio:

width constraint

Therefore, you can't express the following code in Interface Builder:

CGFloat ratio = 1.0;
NSLayoutConstraint *constraint = [NSLayoutConstraint
    constraintWithItem:myView
    attribute:NSLayoutAttributeWidth
    relatedBy:NSLayoutRelationEqual
    toItem:myView
    attribute:NSLayoutAttributeHeight
    multiplier:ratio
    constant:0];
constraint.priority = 1000;
[myView.superview addConstraint:constraint];
like image 91
Jano Avatar answered Nov 04 '22 00:11

Jano


Please add a new constraint, aspect ratio to 1:1 on the UI element as in the image.

Set Aspect ratio to 1:1

like image 3
user550088 Avatar answered Nov 04 '22 00:11

user550088