95

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.

3
  • I remember Peter Ammon talking about "Rely on Interface Builder as much as possible" in WWDC'12. How ironic. Commented Mar 2, 2013 at 23:41
  • 4
    Yeah, I think there are still a few things missing from the implementation of layout constraints. It seems they've given us access to most of the parameters, but not the multiplier, which would be useful for a lot of things. Commented Mar 3, 2013 at 0:40
  • But it's getting better and better! I mean Auto Layout was introduced less than 3 years ago and from my point of view the support is really good nowadays. Commented Feb 10, 2014 at 16:37

4 Answers 4

191

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];
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the latest update. Xcode 5.1 beta 5 actually allows you to set aspect ratio constraints in IB, but when I try to compile the project, I get following error: Aspect ratio constraints with Xcode versions prior to 5.1. So maybe we have to wait for the final 5.1 version. Same for you, @Jano?
Yes, the changes to the XIBs are not backward compatible. If you edit with 5.1 you can't go back to lesser versions.
So you are able to compile the xib? It doesn't work even with Xcode 5.1 beta 5 for me.
@onmyway133 create a constraint between the two views and set the multiplier to 1:2.
In Xcode 7, ctrl+click drag from the view and release on top of itself, and select Aspect ratio
|
3

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

Set Aspect ratio to 1:1

Comments

0

To start, control drag diagonally from the button to itself. A contextual menu will appear, where you can add width and height constraints. Shift+Click on each; a checkmark will appear indicating that you have added the constraint. (If you accidentally dismiss the dialog before adding both, that’s OK, just repeat the drag step and set the other one):

enter image description here

When first added, these constraints take on the current width and height of the button, so you’ll need to adjust each constraint to give it a more appropriate value. We’ll have to do this one at a time, although our image is square, so be sure to use the same constant value in both constraints to resize the button proportionally. Double-click on the constraint, and enter a smaller value in its constant field:

enter image description here

Comments

-1

As gperf the Constand don't has to been changed. For perfekt Hash Funktion and no when -p is bigger then n- there is an actiange that can get that high that it rizes the capatabilletie off an 32 or 64 running one doesn't reach 2200=GB

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.