Search code examples
cocoa-touchautolayoutnslayoutconstraintnib

UITableViewCell nib with several constraint scenarios


I have a UITableViewCell nib that depending on the data it can show an image or not.

Below, the cell has 2 labels and at the right most the imageView. What I want to do is to remove the imageView (if no image is present on the data) and extend the Label all the way to the right.

Is there a way to have several constraint scenarios on the nib that I could activate/ deactivate?

enter image description here


Solution

  • Constraints on label1

    1. leading, 2. center vertically in container, 3. fixed width

    Constraints on label2

    1. Leading space with label1, 2. Center vertically in Container

    Constraints on imageview

    1. Trailing space, 2. Top space, 3. Bottom space,
    2. Width constraint : width <= 50 (When there is no image then width will automatically get zero),
    3. Horizontal space between

      label2 and imageview = 0 (priority = 250)

    See the below GIF:

    enter image description here