2

I have a horizontal stack view in a UICollectionViewCell. There a four items in the stack view, the first three are specialized UILabels (seen as "loose", "$?", and a score of 75), I never want to be compressed. The final item is a specialized UITextView that can list any number of user defined tags, and so can be a very long list.

The first time the cell is used, it lays out everything correctly. Correct layout

But when the cell gets re-used, some of the first two items will be compressed. These are the results with default settings, but I tried various combinations of hugging/compression priority but it never seems to have the correct result. Am I just approaching this incorrectly?

Bad layout

Edit: Some setup info: stack view settings

stack view constraints

StainlessSteelRat
  • 364
  • 1
  • 2
  • 16
  • 1
    "Am I just approaching this incorrectly" Tell us how you're approaching it. Can you show the code that populates the stack view? – matt Oct 23 '20 at 22:53
  • Sorry, I thought it was implied, It's all via storyboard. They are simply UILabels and a UITextView at the end with default hugging/compression priorities. No dynamic adding/removing views to stack view at runtime or anything like that. The stack view is horizontal and takes that full width of the cell from the beginning of "Loose" to the end of "F", ie, it's pinned to left and right sides with a small margin. – StainlessSteelRat Oct 23 '20 at 23:19
  • Added some more setup info. Please let me know if you'd like more! – StainlessSteelRat Oct 23 '20 at 23:24
  • 1
    Try giving exactly one of the arranged subviews a lowered compression resistance priority? – matt Oct 23 '20 at 23:32
  • I tried that, and same results. First time it lays out, it's fine, but subsequent "reloadData" or reuse of cell lays it out incorrectly. I'm kind of at a loss! – StainlessSteelRat Oct 24 '20 at 04:17

1 Answers1

2

If I understand your layout correctly...

  • "Loose" "$?" and "75" are each a custom UILabel
  • "Shell" "A" "C" etc... are custom characters / elements in a custom UITextView

It's not clear, however, what should happen when you have too many "tags" to fit in the horizontal space of the custom text view.

Regardless, if you want the first three elements (custom labels) to always display as they are - no stretching, no compression...

Leave the stack view settings as you have them:

Axis: Horizontal
Alignment: Fill
Distribution: Fill
Spacing: 5

And use these Content Priority settings (the translucent light-blue rectangle is the frame of the custom text view):

enter image description here

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • I finally ended up giving up and just reimplementing those custom labels as more "tags" but thank you! I'm pretty sure I tried setting these correctly, (or the opposite, setting the tags to a lower value than default) and just COULD NOT get it working but thanks for helping! – StainlessSteelRat Oct 26 '20 at 00:26