1
votes

I'm trying to lay out the following simple UI using auto layout: enter image description here

So far, I've set 5 constraints:

  • The left edge of the textfield is constrained to the left alignment guide
  • The top edge of the textfield is contained to the top alignment guide
  • The right edge of the label is constrained to the right alignment guide
  • The top edge of the label is constrained to the top alignment guide
  • The right edge of the textfield is constrained to be 20 points from the leading edge of the label

Based on these constraints, I would think that the width of the textbook would be determined by the size of the label, and horizontal layout for both would be implied. This is indeed how these constraints behave when I change the size of the button, but I'm still receiving the following warning in xcode:

enter image description here

My question is why? Width and horizontal position can be determined by the information provided, and providing additional information (like a fixed width) would only mess with the flexibility of the interface.

1
What compression and hugging priorities do they have?Wain

1 Answers

2
votes
  • Well Width can't be calculated based on the constraints you applied.
  • Both textField and label grow and shrink based on content.
  • What will happen If at runtime I add much more text or increase text(content) of both the fields. Then there will be ambiguity about which field to fill full and which to trim.

  • So one thing need to be fixed based on that other width can be calculated.

  • You can add one Extra width constraint to Add Activity.
  • My extra width constraint would be less than or equal to the width you assign. And its priority would be greater than its leading.

enter image description here

Or you would want to play with content hugging priority. With changing content hugging priority one can make it possible for label not to grow. Cocoa Autolayout: content hugging vs content compression resistance priority

If you don't want to add Extra width constraint Then increasing horizontal hugging of Add Activity to 251 (greater then textfield hugging) will resolve the ambiguous constraint issue.

enter image description here