0

Sorry for the title if it's not the best but I really don't know how to explain this in a few words.

So, what I have is a view with a label and a image. There are two casses. One in which I don't need the image, just the label(the most ussual one). And one where I need both the image and the label and at some point I have to remove the image through an animation(this one is handled).

Now. for the one where I need just the label I was thinking to play with the constraints. I want to have the spacing for the left 15 and the constraint to go to the superview, but also when there is a image I want the constraint to go to the image. I'll add a image to make it more clear. How can I accomplish this?

enter image description here

razvan
  • 355
  • 4
  • 19

2 Answers2

2

Actually you can do the same thing from the XIB also.

Select the leadingConstraint outlet from the XIB and create the outlet for the constraint.

It will create a property of type NSLayoutConstraint, then set the value to 0 in case where you don't want to show ImageView

  self.imageViewLeadingConstraint.constant = 0;

Repeat the same process for ImageView Width also.

1

For that you should bind (Take @IBOutlet) UIImageView's width constrains AND leading constrains and you need to manage it in cellForRowAtIndexPath like

if isOnlyLabel { // "isOnlyLabel" it's just for understanding 
  // Here you have only label not image
  // Set image with constraint = 0 and leading = 0
}
else {
   // Here you have label + image
  // Set image with constraint = 60 and leading constraint = 15 Or as you want
}
Govaadiyo
  • 5,644
  • 9
  • 43
  • 72