2

I am struggling with autolayout in Interface Builder.

I am having the following layout:

-------------------------------
|                             |
|    _____________________    |
|    |                   |    |
|<-->|      UILabel      |<-->|
|    |                   |    |
|    ---------------------    |
|    <------------------->    |
-------------------------------

What I want to accomplish:
I want the UILabel to have a width of 300, unless the left/right margin becomes less then 30. In that case the UILabel's width must be smaller so the left/right margin will always be 30.

What I tried:
UILabel Width: Less then or equal: 300
Left/Right Leading/Trailing: Greater than or equal: 30

This works in layouts that don't fit the 360 width, so the UILabel becomes smaller. However, when I have like 400 width available, the UILabel becomes 280 and the Leading/Trailing becomes 50...

I have experimented with different priorities, but nothing seems to have a real effect. I also tried to set the UILabel width constraint to equal, but that did not work on widths smaller then 360.

Is there a solution to this problem?

Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • Try setting the leading/trailing constraints simply equal to 30 but set the priority of your width constraint to 750. This way, no matter what the size of the view is, the left and right constraints will be 30. – Prientus Mar 13 '17 at 21:32
  • look at [hugging priotity](http://stackoverflow.com/questions/15850417/cocoa-autolayout-content-hugging-vs-content-compression-resistance-priority) this should solve your problem. – Axel Mar 13 '17 at 21:57

2 Answers2

4

Try this... on your label:

  • set both Leading and Trailing to Greater than or equal: 30
  • set Width to 300, with Priority of 999

enter image description here

enter image description here

That should do what you need.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Yep I confirm, this is what I would do first. Could you tell us if it worked? – Gil Sand Mar 13 '17 at 23:00
  • It didn't work at first, but I removed the label and readded it, and then applied your solution. Now it works perfectly! – Peterdk Mar 14 '17 at 14:07
  • Does not work with Xcode 9. Could you please confirm? – SRP-Achiever Oct 18 '17 at 22:09
  • @SRP-Achiever - *"Does not work with Xcode 9"* - you'll need to be a little more descriptive... I just tried it with Xcode 9, Deployment Target iOS 11.0, and it seems to do just what was expected. – DonMag Oct 19 '17 at 23:26
  • It works because you have centered the view. Try removing the horizontal center constraint and let me know if it works? – SRP-Achiever Oct 25 '17 at 14:54
  • 1
    Well, yes, the whole purpose is equal space (min 30-pts) on each side, with the label being max 300-pts in width. If you're trying to do something different, then no, I wouldn't expect this to do whatever it is you're trying to do. – DonMag Oct 25 '17 at 15:14
0

Give UILabel a width of 300 and set priority to High. Left/Right Leading/Trailing : 30 with Required priority. This should work.

NotABot
  • 516
  • 1
  • 8
  • 27