Rob85
Rob85

Reputation: 1729

understanding Xcode 7 constraints

If possible i would like a little clarification on how the constraints work for a TextView.

I have added a textView that sits under a button on my view. For a while i could not get it to display correctly.

Forgetting Height and Width for a minute i was under the impression that i could at least centre the TextView by adding a horizontal constraint. This does not seem to be the case. So i added a constraint for the top being 30 away from its neighbour.

So in my head the TextView should be centred on the x axis and 40 below the button. Again there was nothing displayed.

Now back to the Height and Width. Only when adding these two constraints does my TextView now centre correctly.

Question 1 is this correct behaviour and why. I seem to be able to display a button with out using height and width constraints.

Question 2 what implications are there for not implementing all the constraints that Xcode prompts you to do. With some of my work i have added some constraints which appear to display correctly yet Xcode would still like more. For example for a textBox if i put a horizontal constraint in Xcode still wants and x position or Width constraint, Even though that TextBox size is fine on both my iPhone and iPad.

Upvotes: 0

Views: 164

Answers (1)

matt
matt

Reputation: 535557

why. I seem to be able to display a button with out using height and width constraints.

Because a UIButton has instrinsic height and width constraints. They are of lower priority than explicit height and width constraints that you provide, but if you don't provide any, then the intrinsic constraints take effect.

what implications are there for not implementing all the constraints that Xcode prompts you to do.

Usually, disaster. If you don't supply enough constraints, you have ambiguous constraints. You might get the result you were hoping for, but you might not. With luck, you will see nothing at all. It isn't worth the risk. In general, believe Xcode. It knows more than you do.

Upvotes: 1

Related Questions