How to align Custom cells to Right Detail and Basi

2019-09-07 10:35发布

问题:

I am using static cells with 4 sections to create a Settings scene:

For some cells I use the predefined styles: Right Detail and Basic.

But for the following cells I use Custom style with my own constraints:

  • Label on the left and Switch on the right side (like the cell selected in above screenshot)
  • Slider occupying the whole cell

My problems: I can not get the leading alignment right and also my 2-lines word-wrapping Label pushes a Switch to the right:

As you can see at the top screenshot, my constraints for the problematic Label in Custom cell are:

  • Align Center Y to Superview: 0 pixels
  • Leading Space to Superview: 2 pixels (does not match Right Detail and Basic)
  • Trailing Space to Switch equal or greater than: 4 pixels (still pushes the Switch too far to the right)

Please help, I wonder how other apps with Settings screens do it.

SOLUTION:

  1. I have unchecked Constrain to Margins and set Leading Space to 15 pixels (thanks Andre)
  2. I have solved my 2nd problem by lowering Horizontal Content Compression Resistance Priority of the Label to 250

回答1:

If you use auto-layout,

you can turn on "Preserve Superview Margins" in

  1. Content View
  2. Your Label & Switch
  3. Your custom Table View Cell

and leave Label's leading space and Switch's trailing space to 0 (default)