简体   繁体   中英

iOS: Multi-line UILabel has wrong size with another UILabel in a horizontal UIStackView

I got a weird issue and I suspect it's a bug in UILabel, but I need to be sure first it's not my bug.

The hierarchy and the problem(s) : The title label gets small and on multiple lines and the year label is too large, even though there is enough space. The next images will show that.

What's important: Title label is multi-line, the rest are out of the box components. Their content hugging and compression resistances are the same, defaults.

There are two situations when this doesn't happen. 1. If I remove the year label 2. If I embed the title label in a stack view of any kind

So just by doing one of these two changes, the title label resizes properly, which is why I suspect it's an UIKit bug. I tried everything I could think of, including changing content hugging and compression resistances, stack view alignments and distributions, view modes.

Setting the prefferedMaxLayoutWidth for the title label doesn't work because the text is dynamic.

There is indeed a bug, for which there are at least two workarounds:

  1. Embed the multi-line label into another UIStackView, as Steven Van Impe proposed.
  2. Change its compression resistance to 999 or 1000.

In the end, I was better off not using stack views due to their limitations. Maybe they'll get better with future updates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM