title: UITableViewCell subclass, wrong height until scroll
body: <p>I have a custom uitableviewcell in which I use a UIImageView. I want the cell to be able to resize its height according to a certain aspect ratio, based on the imageview width, but I keep getting these constraints problems:</p>
  
  <pre><code>"&lt;NSLayoutConstraint:0x7ffe13c14130 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ffe117fb920(44)]&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15090 V:|-(0)-[UIView:0x7ffe13c11c10]   (Names: '|':UITableViewCellContentView:0x7ffe117fb920 )&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c150e0 V:[UIView:0x7ffe13c11c10]-(0)-[UIButton:0x7ffe117e8110]&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15190 V:[UIButton:0x7ffe117e8110(30)]&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c151e0 V:[UIButton:0x7ffe117e8110]-(0)-[UIView:0x7ffe13c11d00]&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15910 UIView:0x7ffe13c11d00.height == UIView:0x7ffe13c11c10.height&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15960 V:[UIView:0x7ffe13c11d00]-(0)-[UIButton:0x7ffe13c0b000]&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c159b0 UIButton:0x7ffe13c0b000.height == UIButton:0x7ffe117e8110.height&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15a50 V:[UIButton:0x7ffe13c0b000]-(0)-[UIView:0x7ffe13c11df0]&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15aa0 UIView:0x7ffe13c11df0.height == UIView:0x7ffe13c11c10.height&gt;",
  "&lt;NSLayoutConstraint:0x7ffe13c15af0 V:[UIView:0x7ffe13c11df0]-(0)-|   (Names: '|':UITableViewCellContentView:0x7ffe117fb920 )&gt;"
  </code></pre>
  
  <p>My constraints are</p>
  
  <pre><code>    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[s1(&gt;=10)][pictureView][s2(==s1)]|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[s3][markAsFavorite(==30)][s4(==s3)][markForDeletion(==markAsFavorite)][s5(==s3)]|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[s1][s3]|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[pictureView][s3]|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[s2][s3]|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView]-10-[markAsFavorite(==30)]-10-|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView]-10-[markForDeletion(==30)]-10-|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView][s5]-10-|" options:0 metrics:nil views:dict]];
      [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView][s4]-10-|" options:0 metrics:nil views:dict]];
  
  
      photoHeight = [NSLayoutConstraint constraintWithItem:pictureView
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:0
                                                    toItem:nil
                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                multiplier:1
                                                  constant:(5*pictureView.frame.size.width)/16];
  
  
      [self.contentView addConstraint:photoHeight];
  </code></pre>
  
  <p>These constraints are added only once, in the updateConstraints method of the cell subclass. In the "drawRect" method I update the "photoHeight" constant and force a layout update.</p>
  
  <p>The Result:
  At first load, the cells look pretty bad, but after I scroll they update their height and look as expected. I think the problem is with the "photoHeight" constant not being updated when the cell is first loaded, but I don't know how to fix it taking into consideration that it depends on the imageview's width.
  Can you help solve this problem?</p>
  
  <p>I attached you some screenshots: 
  Before scrolling
  <img src="https://i.stack.imgur.com/TRdXN.jpg" alt="before scrolling"></p>
  
  <p>After scrolling
  <img src="https://i.stack.imgur.com/rsqia.jpg" alt="after scrolling"></p>
  
  <p>As you can see in the first screenshot, the image gets out of cells contentview, because the cell is to small for that image...but, after scrolling the cells are resized correctly. I want them to be resized from the beginning, not only after scrolling. </p>