Search code examples
iosswiftuitableviewautolayoutnslayoutconstraint

How to add constraint to right align an image inside UITableViewCell


Problem : I am trying to set constraints in storyboard for an image so that it is always aligned to the right inside UITableViewCell

I am setting the trailing space, top and bottom space, height and width of the image.

The image then goes outside the tableview. (Screenshot-1)

The applied constraints from attribute inspector view is shown in screenshot-2

I am using Xcode Version 11.0

enter image description here

enter image description here

What i tried:

1) Tried suggestions from this stack overflow thread Constraints to be set for x position This is close to what i am looking for but doesn’t seem to work in my case.

2) Set up trailing space, top and bottom space, height and width of the image.

3) Enclosed the image in a UI View and tried step 2

4) Set up only top or bottom space to the container, add width, height constraints

5) Tried applying space constraints to the table cell rather than the container.

In all the cases, the image goes outside the tableview.

Question

How can satisfy the constraint in such a way the image is aligned to the right inside UITableViewCell.

Please let me know if i am missing something or if you need any other information to provide your inputs.

Screenshot on clicking the constraint

enter image description here


Solution

  • Click the trailing constraint and change it's firstItem attribute to leading instead as currently you do

    trailingOfImage + 7 = leadingOfCell

    Which make it outside the cell