2

For a week I have been struggling with a 'simple' piece of layout in a storyboard. I want three labels which all have numberOfLines set to two.

The UIStackView has some constraints to position it in the table cell. The two left labels have a width constraint set to <= 100 to make sure they don't stretch out too far. I have been playing a lot with the content hugging and compression resistance and with things like setNeedsLayout or layoutIfNeeded. You can see the problem in the screenshot I added. There is barely any code written in the ViewController.

When you check out the test project I added and run it on the simulator you will notice when scrolling up and down the cells will all start to look the same and the text is no longer truncated. That's exactly what I want.

Here is a link to the test project I am working in.

Result

1 Answers1

4

In Prototype Cell set horizontal and vertical Content Hugging Priority to 1000 and the same for Content Compression Resistance Priority to 1000 (Left Label and Middle Label) and the result is what you are probably looking for, make Left and Middle labels as small as they can be and the right one to fill the gap...

Simulator Screenshot

Ladislav
  • 7,223
  • 5
  • 27
  • 31
  • Thank you so much for helping me out! Could you explain why the priority of 1000 works? Has this anything to do with other constraints that are 1000? – Daan Vermeulen Oct 13 '17 at 13:55
  • 1
    Making a constraint have a priority of 1000 means that it is basically required. Sometimes when doing more complicated layouts you can make some constrains optional with lower priorities, meaning if system has two constraints that it cannot satisfy it will be able to drop the constraint with lower priority, if both have the same priority it will drop one based on some logic and notify you in a log "Unable to simultaneously satisfy constraints.". – Ladislav Oct 13 '17 at 14:06
  • As far as `Content Hugging Priority` and `Content Compression Resistance Priority` goes check out this answer where it is nicely explained what each one does https://stackoverflow.com/questions/15850417/cocoa-autolayout-content-hugging-vs-content-compression-resistance-priority – Ladislav Oct 13 '17 at 14:08
  • 1
    Great explanation! I have a better understanding, now I can chill this weekend and let it go! – Daan Vermeulen Oct 13 '17 at 14:37