4

I'm trying to figure out how to align a label in a custom UITableViewCell so that it starts exactly with the row separator using AutoLayout in Interface Builder (default insets).

My problem is that the separator inset is different on the iPhone and the iPad so I can't just use a fixed spacing with a "Leading Space to Container Margin". If I do, the label will be out of place on one of the 2 device types.

Is it even possible to tie a constraint in Interface Builder to the table separator inset?

enter image description here

Mark
  • 6,647
  • 1
  • 45
  • 88

2 Answers2

4

Finally found the correct answer here: https://stackoverflow.com/a/31133547

The key is to check Preserve Superview Margins on the cell's contentView and the cell itself.

Then set up the margin layout constraints with a constant of 0, equal height of your custom subview and center it vertically in the contentView.

Community
  • 1
  • 1
Mark
  • 6,647
  • 1
  • 45
  • 88
1

Tying a constraint to the separator inset value in Interface Builder is not possible. You'll need to dynamically add a constraint at runtime based on the separator inset of each cell.

You can retrieve the inset value by examining the separatorInset property on the appropriate UITableViewCells.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45