Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout table cell with image and text fields?

I'm looking to do a table view cell with a photo and wrapping text with auto layout. I've managed a lot with auto layout, but I'm at a loss on how to do this one.

┌─────────────────────────────────────────────────────┐
│ ┌──────────┐ ┌────────────────────────────────────┐ │
│ │          │ │ Title (might wrap)                 │ │
│ │          │ └────────────────────────────────────┘ │
│ │  Photo   │ ┌────────────────────────────────────┐ │
│ │          │ │ Body text. May wrap, may contain   │ │
│ │          │ │ multiple lines.                    │ │
│ │          │ └────────────────────────────────────┘ │
│ └──────────┘                                        │
└─────────────────────────────────────────────────────┘

As the text expands, I want to keep the photo in the top left and expand the cell. However, the cell should never shrink below the minimum required to show the photo (with margins).

┌─────────────────────────────────────────────────────┐
│ ┌──────────┐ ┌────────────────────────────────────┐ │
│ │          │ │ Title (might wrap)                 │ │
│ │          │ │ Maybe even to two lines.           │ │
│ │  Photo   │ └────────────────────────────────────┘ │
│ │          │ ┌────────────────────────────────────┐ │
│ │          │ │ Body text. May wrap, may contain   │ │
│ │          │ │ multiple lines.                    │ │
│ └──────────┘ │                                    │ │
│              │ Text could require more vertical   │ │
│              │ space than the photo.              │ │
│              └────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘

I'm requiring iOS 8 or later.

I've tried doing this with just the three views listed, and also by putting the left item and right items in two views. Every combination I try seems to ignore the photo's minimum size.

(Btw, for anyone curious: Graphic via Monodraw.)

like image 943
Steven Fisher Avatar asked May 11 '15 22:05

Steven Fisher


1 Answers

Try having the following relationships:

Photo:

  • width = X
  • height = Y
  • left = Cell + Z
  • top = Cell + Z

Title:

  • left = Photo + Z
  • right = Cell - Z
  • top = Cell + Z

the label will auto calculate the height based on content

Body:

  • Left = Title
  • Right = Title
  • Top = Title.Bottom + Z

Cell: (this is the important part)

  • Bottom >= (equal to or greater than) Body.Bottom + Z
  • Bottom >= Photo.Bottom + Z

this will force the cell to be, at all times, either taller than the image + offset or taller than the label + offset

like image 67
Danny Bravo Avatar answered Nov 07 '22 12:11

Danny Bravo