Search code examples
iosautolayoutconstraintsxcode-storyboard

Trailing constraint


I'm currently learning about auto layout and trying to setup some table cells to show the same across devices

The layout shows correctly across iPhone devices, but on iPads its stretched.

I believe I've narrowed it down to the trailing constraint, when I remove it it displays correctly on both device types but Xcode throws a missing constraint warning.

The constraints are using less than or equal, which works fine on iPhone but doesn't display correctly on iPad. iPad works if I use more than or equal, but this messes iPhone up

These are the constraints enter image description here

This is how it should appear on both devices. enter image description here

But this is how it appears on iPads enter image description here


Solution

  • The reason you are getting a wide text field on iPad is because you're constraining the "Partial Bag" text field to the trailing edge of the cell.

    Without looking at your actual Storyboard, not sure why you're getting constraint conflicts.

    This is how I set up your cell, and have no issues (other than the known bug with text vertical alignment in IB when laying out rounded-border text fields):

    enter image description here

    Note that I gave Full Bag a Width of 32, and constrained the width of Partial Bag to be equal to Full Bag. No constraints to the Trailing edge of the cell's content view.

    The result, in Portrait and Landscape orientations (which will, of course, layout similarly on an iPad):

    enter image description here

    enter image description here