1

Inside of a uitableviewcell I have an image view and a label side by side. I'm using systemlayoutfittingsize + a sizing cell to drive the table view cell height via autolayout for ios7/8. I'm also using setting up these constraints programatically (no storyboard answers please). Two possible scenarios...

Scenario 1: Label height is intrinsically smaller than image height

Desired result: Size of the cell expands so that the image (set explicitly to 100 width, 75 height) is centered vertically in the bounding area, and the label's top edge is aligned with the images top edge.

enter image description here

Scenario 2: Label height is intrinsically greater than the image height

Desired result: Size of the cell expands so that the label is centered vertically in the cell. The image's top edge aligns with the label's top edge.

enter image description here

Msencenb
  • 5,675
  • 11
  • 52
  • 84

1 Answers1

0

So the constraints needed for this appear to be

For the image view: 1. Pin width = 100 2. Pin height = 75 3. Pin leading space to superview = 10 4. Pin top space to superview = 10

For the label: 1. Pin top space to superview = 10 2. Pin bottom space to superview = 10 3. Pin width = 200 4. Pin trailing to superview = 10 5. Pin horizontal spacing = 10 (between label and image view)

Then in the code all we have to worry about is the "heightForRow" UITableView delegate method. Since we did not set a static height for the label and we pinned it to the top and bottom of the superview/cell it will change size dependent on the height of the UITableView cell. We have to calculate the "numberOfLines" for the dynamic length of the text of the label. There are many links out there that can help you with this - How to calculate UILabel height dynamically?

Community
  • 1
  • 1
Ross Barbish
  • 680
  • 6
  • 22
  • I'm using code, not storyboard for setting up the constraints. The issue turned out to be one of setting preferredMaxLayoutWidth on the multiline label, although it's not fully fixed yet. – Msencenb Nov 21 '14 at 19:38