0

I'm trying to use AutoLayout but I have some problems. This is my Custom Cell with an UIImageView on the top left, a UILabel (Title), a UITextView(SampleText) and another UIImageView at the bottom:

enter image description here

My problem is that everything is calculated correct, but in some cases I don't need the UIImageView at the bottom, and than the other Views are not on the correct place anymore if the UIImageView is removed. Is it even a good idea to use AutoLayout in this case?

Peter Foti
  • 5,526
  • 6
  • 34
  • 47
Davis
  • 1,253
  • 4
  • 17
  • 38
  • 2
    It is almost always a good idea to use auto layout. In this case, where one view is sometimes not present, I would set up the constraints in code, where some of the constraint code varies between scenarios. – Marius Waldal Feb 18 '14 at 14:06

2 Answers2

1

You don't need to setup constraint in the code, just set them up in Interface Builder and make IBOutlets out of them.

Then, when your imageView isn't needed, just set its width and height to zero and adjust the constraints accordingly.

Something like this:

if (!showMyImage) 
    GCRect tmpFrame = image.frame;
    tmpFrame.size.width = 0;
    image.frame = tmpFrame;
    [iboutletConstraint setConstant:0]
John Green
  • 504
  • 2
  • 7
1

I would suggest to add extra bottom space equals constraints to left image and text view with low priority. While bottom image view is presented, autolayout system respects its constraints (because priority is higher). When bottom image view is removed, its constraints disappear, and autolayout system respect constraints with lower priority.

vokilam
  • 10,153
  • 3
  • 45
  • 56
  • Perfect, thanks :) it set the Priority of the two bottom space constraint to 999 and it works like charm, sadly now when no image is set, the whole cell is too big, any chance to calculate the right size for the whole cell with autolayout and without using -heightForRowAtIndexPath? – Davis Feb 18 '14 at 14:37