iOS layout why does center constraint not enough? - ios

I really don't understand the logic of this ... well ... strange iOS layout system.
I place a UILabel with the constraint Align center X to Superview.
Why does it complain to need a new constraint for X position ??
center is center no?

You need to tell more about the horizontal positioning of the view. Centering the view does not say wether it has a fixed width or it should keep the leading / trailing space around view.
Here are two possible options (there is also a fixed height with a fixed aspect ratio, as #Moshe said in his answer):
Your view will always have a width of 61 (for example):
Your view will have a fixed leading and trailing space on the left/right side:

That warning is a misnomer. It doesn't mean there's no constraint. It should say that "there aren't enough constraints to calculate an X position."
You need to add a constraint for X the position, but you also need to give enough constraints to for auto layout to figure out the width of your view.
You can either provide a Leading Space and a Trailing Space, or you can provide a Width, or you can provide a Height and a Ratio. Autolayout just needs something to work with.

If you actually have added a center x constraint, that should be enough for auto layout to calculate the x position. But are you sure you didn’t set a center y constraint instead? The directions that you drag the lines in Interface Builder can be confusing, so you might have accidentally set a y constraint when you meant to set an x constraint. If you are trying to center x, you will still need a constraint for the y position, whether it’s vertical centering or space from top or bottom to the superview or some other view.

Related

iOS Layout Constraints | Width anchor vs leading and trailing anchors

I haven’t been using AutoLayout constraints for long, so I haven’t fully grasp some of its inner workings.
In particular, are there times I can use the widthAnchor and the leading and trailing anchors interchangeably, and others that I need all 3? Or, is valid to use the widthAnchor with the leadingAnchor or the trailingAnchor?
To sum up my question, is the combination of the leading and trailing anchors just defining the width and the X axis position of a view, just as the widthAnchor with the leadingAnchor would?
Basically, the auto layout needs at least two constraints for views having an intrinsic size and at least four for views without having an intrinsic size. Let's say your view has an intrinsic size(eg. UITextView), it's just enough to constraint the x-axis and y axis. If you wish, you can specify the fixed size with width anchor and height anchor or dynamic size with constraining top, bottom, leading, trailing anchors based on the screen size but this is just optional. If your view doesn't have an intrinsic content size(eg. UIImageView), you need to specify at least one x-axis, one y-axis and width and height anchor or constraint leading, trailing, top and bottom anchors. The point is if you have the leading and trailing anchors, you don't need to specify width anchor and vice versa. Now, you get the idea. If you constraint top and bottom anchor, you shouldn't add height anchor to avoid conflicts.

set label position with priority constraint?

I have one label to the right side of parent view.On top of label there is a view.I have given below constraints to label.
Align leading to top view (constant<16) LOW PRIORITY
Top space from parent view
Trailing space to parent view (constant=28) HIGH PRIORITY
I want when text inside the label increase it should change it's position.it should move towards left but at the same time it should also move towards right.I mean should increase it's width in both directions.As of now it's only moving towards left & keeping fix position from right.
Please tell how can i achieve this task?
i think if you set center alignment for label alignment you should get answer you want.Also give same priority to leading and trailing.
This will not work as the trailing space to super view constraint will most likely always win, as you have observed.
The width and height of the label is taken care of by its intrinsic content size and hence no constraints are needed for that. As far as position goes, the y is taken care of by the top space constraint. Finally the x will need a constraint that will allow it to grow in both directions as mentioned. To enable this behaviour remove the leading and trailing constraints and add a horizontally centered constraint. This will centre the label to its super views centre. To offset the position left or right you may choose a constant value or use the multiplier property.
The multiplier is good for positioning a certain percentage of the superview regardless of its size. e.g.: if the superview is the root view then a multiplier value of 0.5 places the label quarter of the way across the view, 1.0 makes the centre x align exactly, 1.5 makes it 3/4 across from the left.

iOS: Constraining View with fixed aspect ratio to trailing edge OR bottom edge

I have an ImageView that is constraint to be a square and on its top edge:
http://i.imgur.com/dACmwPN.png
The problem is that I want the view to fill the entire bottom space without exceeding the superview. In other words it should conform to either the width or the remaining height depending on which one is smaller.
The guide I am following suggests to add all of these to a stack view, but that is an iOS 9 feature and I want to develop for iOS8. What would be a good solution for that SDK?
I would add width = height constraint to keep the aspect ratio, then bottom and trailing constraints with lower priority (900) and >= margin value (ex 8) . This way image will be a square and it will not go over bottom or right. Also aspect fit should be set.

Autolayout bottom spacing constraint is not obeyed

I have a UIButton and a UILabel constrained to be a standard distance from the bottom of the Superview. Works well on the iPad, but on the smaller iPhone screens, when other elements take up too much space, these views are pushed off the edge of the screen despite their constraint to remain a standard distance from the bottom. Why is this so?
What I would like to have happen is for the four rectangles to shrink in size so that there is still room for the "Go Back" and "Question" label to remain a standard distance from the bottom. The four rectangles can maintain aspect ratio and equal width/height by all shrinking at the same ratio. I have no constraints on their needing to be equal to or larger than a certain size. I've tried lowering their Content Compression Resistance Priority as well.
Configuration:
(I have also tried "equal" and "<=" in top spacing between "Go Back" & bottom left rectangle)
("Greater than or equal" works best on iPad to keep "Go Back" at the bottom of the screen)
How it looks on iPhone 6 and iPhone 6+ - with the labels cut off at bottom:
Did you try to lower those four buttons' height constraint's priority??
For example like this, try to set them to 750
You can make this work with a couple of changes and additions. Give the leading and trailing constraints between the top 2 rectangles and the superview a lower priority (I used 749), but still keep them as "equal". This will keep them at the standard distance from the edges if it's possible, but will allow them to have a larger spacing if the vertical space combined with the aspect ratio requires it. The problem with this, is that since they aren't required any more, when those constraints need to stretch, there's nothing that says they have to stretch equally; therefore, we need some way to keep the rectangles centered. So, instead of a spacing constraint between the left and right top rectangles, add a small view (I used 8x8) that has a centerY constraint to one of the rectangles, and zero constant spacing constraints to the two rectangles. Give this view a centerX constraint to the superview; this construct will give you the same spacing between your rectangles that you had before, but will keep them centered in the superview while allowing them to shrink in width (and height to keep the aspect ratio) if need to accommodate the vertical space.

Align multiple images equally in different screen size

I am having troubles aligning multiple images by using auto-layout in xcode.
I tried different settings, but nothing seems to work yet (refer to attached photos).
It would be great to hear some opinions from you guys, either in code or storyboard.
I want the image to be aligned equally in different screen sizes.
Too much space
Last image scaled too much
When Equally width is set
Constraints Setting
You need extra views to achieve this
In the sample picture the red rectangles they are all invisible UIView with constraints:
fixed length leading space to the view on the left(or the superview)
fixed length trailing space to the view on the right(or the superview)
fixed length height
This way it's the invisible views who have different width in different screen size while the size of images between them is fixed.
For left most item you should specify leading space to superview, for right most specify trailing space to superview. For all inner gaps between views specify horizontal spacing and for each neighbor view specify equal width constraint. Additionally I recommend for you to specify Align center Y for all views and set the y position constraint only for one of them

Resources