Stack108 Stack108 - 1 month ago 20
iOS Question

xcode auto layout issue

in my swift 2 app i working with auto layout.
but at the moment i have a problem, which i can't solve.

it can be a simple problem for you, but for me it is hard to understand.

enter image description here

There is an label in a view controller, which has a vertical center position and a width of 240.
I understand the "problem", but i don't know, how can i solve it ...

Answer

Auto Layout can be confusing, especially at first. You should take some time to watch or read a good tutorial on the web.

Probably you dragged the label onto the canvas, set some constraints, and then tried to resize the label to look the way you wanted it to.

The problem is that while this changes what you see on the canvas, the constraints haven't changed. The warnings are there to let you know that what you're seeing is not reality.

You can get rid of the warnings easily by right-clicking and choosing the option to fix misplaced views.

What this does is update the canvas to match the constraints you've set. Your label will probably move and change size.

Now, chances are that you won't like what you see after doing this. But the thing to do now is change the constraints, not the label on the canvas.

One thing worth noting is that labels have an intrinsic size. That means that the width and height of the label itself are determined by the text inside.

Typically you only need to set one constraint to position the label horizontally, and another to position it vertically. In fact you might try to see if you can center your label horizontally and vertically first, then proceed to work on positioning it how you really want it.