title: Dynamic UITableViewCell's height in iOS 7 AND iOS 8 impossible
body: <p>I'm having a hard time trying to get a table view with dynamically sized cells to be layed out well under iOS 7 and 8. I can't detail all the differences that are to find (in the means of "ways of broken layouts") between iOS 7 and 8 which can be produced using the different "tweaks", "workarounds" and whatever I've found here and elsewhere. But in the end either on iOS 7 or on iOS 8 (if not both) some or all cell's content is misaligned because the layout system "breaks" one of the custom constraints to "recover".</p>
  
  <p>Basically I have three different types of content. And as I show these contents not only in the aforesaid table view, I wrapped the contents in three subclasses of <code>UIView</code>. Let's call them <code>SummaryView</code>s
  For the table view then I created three subclasses of <code>UITableViewCell</code> of which each adds the according <code>SummaryView</code> to its <code>contentView</code> and sets <code>self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight</code>. On <code>updateViewConstraints</code> I generally remove all "my" constraints that might have been added previously and do...</p>
  
  <pre><code>- (void)updateConstraints
  {
     // ...removed custom constraints before
      NSDictionary *views = NSDictionaryOfVariableBindings(_summaryView);
  
      [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_summaryView]-|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:views]
                          toView:self.contentView];
  
      [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_summaryView]-|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:views]
                          toView:self.contentView];
      [super updateConstraints];
  }
  </code></pre>
  
  <p>In <code>tableView:estimatedHeightForRowAtIndexPath:</code> I return a static estimate depending on the content's type.
  In <code>tableView:heightForRowAtIndexPath:</code> I use "prototype" cells in the manner of...</p>
  
  <pre><code>// ..set content on prototype cell before
  [prototypeCell setNeedsUpdateConstraints];
  [prototypeCell layoutIfNeeded];
  CGSize size = [prototypeCell systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
  return size.height;
  </code></pre>
  
  <p>Here usually the debugger breaks on <code>UIViewAlertForUnsatisfiableConstraints</code> (on <code>[prototypeCell layoutIfNeeded]</code>) with <code>&lt;NSLayoutConstraint:0x17409f180 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x17019f070(44)]&gt;</code> conflicting with the rest of the constraints.</p>
  
  <p>So I tried...</p>
  
  <ul>
  <li>Setting <code>tableView.rowHeight = UITableViewAutomaticDimension</code> in table view's <code>viewDidLoad</code></li>
  <li>Not implementing <code>tableView:estimatedHeightForRowAtIndexPath:</code></li>
  <li><p>Using...</p>
  
  <pre><code>if (NSFoundationVersionNumber &lt;= NSFoundationVersionNumber_iOS_7_1) {
      cell.contentView.frame = cell.bounds;
      cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
  }
  </code></pre>
  
  <p>...before applying cell's content and calculating its layout</p></li>
  <li>Doing <code>self.contentView.bounds = CGRectMake(0.0, 0.0, 1000, 1000);</code> upon cell initialization</li>
  <li>Probably other things which, right now, I can't remember as I'm on this for more or less two days now.</li>
  </ul>
  
  <p>It's always the same, if I achieve a variant which doesn't complain about unsatisfiable constraints, the layout is usually messed up nevertheless. And although I get the table view not to set the "default" <code>&lt;NSLayoutConstraint:0x17409f180 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x17019f070(44)]&gt;</code> constraint, the constraint set instead (on basis of the height returned by <code>tableView:heightForRowAtIndexPath:</code>) neither suits the layout system.</p>
  
  <p>This post is kind of a last resort. I know that without the concrete view classes' constraints you can't re-check them. But as I'm able to use these views's outside of a table view without problems, it shouldn't be a subview's constraint's problem.</p>
  
  <p>I think I will have fall back to calculate the sizes of all view elements manually (on basis of <code>[UIScreen mainScreen].bounds</code>) and set them directly (as widths and heights) to all subviews. So I'm able to get the concrete height of a cell and can set the <code>contentView</code>'s frame manually. Quite a pity as it messes up the layout code remarkably....</p>
  
  <p>Best regards,
  gabriel</p>