0

I have some problems with autolayout and UICollectionViewCell . You can see cell prototype scheme below. Green rectangle is UILabel and 2 red rectangles are UIImages.

I have defined following constraints:

  • UILabel is centered in cell
  • UILabel has fixed top spacing to cell (5pt)
  • UIImages and UILabel has center Y alignment
  • UIImage and UILabel has defined horizontal spacing (5pt)

My goal is: I need to stretch my label according to its contentSize, so UIImages are exactly 5 pt from text, not from UILabel border.

Is it possible without calculating in code and assigning values to width constraint?

Cell image

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
user2786037
  • 495
  • 1
  • 12
  • 27

1 Answers1

0

If your green label is multiline, you can use preferredMaxLayoutWidth property of UILabel.

Example:

self.myLabel.preferredMaxLayoutWidth = 300; // or whatever value you like

For more details look at iOS: Multi-line UILabel in Auto Layout

Community
  • 1
  • 1
Nikita Took
  • 3,980
  • 25
  • 33