title: Autolayout + constraints for dynamic table height based on total rows
body: <p><strong>First of all this ain't about dynamic cell's height. So don't mix it up.</strong></p>
  
  <p>I have a scenario in that I created three cards </p>
  
  <ol>
  <li>Detail card: Show specific details for location</li>
  <li>Chart card: Show different charts based on selection</li>
  <li>More details card: Card shows more details</li>
  </ol>
  
  <p>Below are screens for above cards:</p>
  
  <p><a href="https://i.stack.imgur.com/QhyqB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QhyqB.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/02XT3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/02XT3.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/vea4V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vea4V.png" alt="enter image description here"></a></p>
  
  <p>View hierarchy for above screens:</p>
  
  <blockquote>
    <ul>
    <li>Controller
    
    <ul>
    <li>ScrollView</li>
    <li>Card-1</li>
    <li>Card-2</li>
    <li>Card-3 &lt;---- We are interested in this card
    
    <ul>
    <li>Custom View
    
    <ul>
    <li>TableView</li>
    </ul></li>
    </ul></li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p>So I'm showing the constraints for last card here, all above card works perfect! and there is no constraints ambiguity or warnings. </p>
  
  <p>Let's go through code:</p>
  
  <p><strong>First Add <code>Card-3</code> to <code>ScrollView</code>:</strong> here <code>chartBox</code> is Card-2 and <code>bottomBox</code> is Card-3</p>
  
  <pre><code>[self.scrollView addConstraints:[NSLayoutConstraint
                                   constraintsWithVisualFormat:@"V:[chartBox(200)]-(10)-[bottomBox]"
                                   options:0
                                   metrics:nil
                                   views:@{@"bottomBox" : self.bottomBox, @"chartBox" : self.chartBox}]];
  
  // Below constraint pin to increase content-size
  [self.scrollView addConstraints:[NSLayoutConstraint
                                  constraintsWithVisualFormat:@"V:[bottomBox]-(10)-|"
                                  options:0
                                  metrics:nil
                                  views:@{@"bottomBox" : self.bottomBox}]];
  
  [self.scrollView addConstraints:[NSLayoutConstraint
                                  constraintsWithVisualFormat:@"H:[bottomBox]-(10)-|"
                                  options:0
                                  metrics:nil
                                  views:@{@"bottomBox" : self.bottomBox}]];
  </code></pre>
  
  <p><strong>Second add <code>CustomView</code> to <code>Card-3</code>:</strong> Here <code>bluePrintView</code> is CustomView</p>
  
  <pre><code>_bluePrintView = [[BluePrintView alloc] init];
  self.bluePrintView.translatesAutoresizingMaskIntoConstraints = NO;
  [self.bottomBox addSubview:self.bluePrintView];
  
  [self.bottomBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bluePrintView]|" options:0 metrics:nil views:@{@"bluePrintView":self.bluePrintView}]];
  [self.bottomBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[bluePrintView]|" options:0 metrics:nil views:@{@"bluePrintView":self.bluePrintView}]];
  
  CGRect screenRect = [[UIScreen mainScreen] bounds];
  CGFloat screenWidth = screenRect.size.width - 20;
  NSDictionary *metrices = @{@"width" : [NSNumber numberWithFloat:screenWidth]};
  [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[bottomBox(width)]" options:0 metrics:metrices views:@{ @"bottomBox": self.bottomBox}]];
  </code></pre>
  
  <p><strong>Third add <code>TableView</code> to <code>CustomView</code>:</strong></p>
  
  <pre><code>self.tableView.tableFooterView = [[UIView alloc] init];
  
  [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:@{@"tableView":self.tableView}]];
  [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:nil views:@{@"tableView":self.tableView}]];
  
  // Height constraint
  CGFloat viewHeight = self.bounds.size.height;
  self.tableHeightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
                                                            attribute:NSLayoutAttributeHeight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:NSLayoutAttributeNotAnAttribute
                                                           multiplier:1.0
                                                             constant:viewHeight];
  [self addConstraint:self.tableHeightConstraint];
  
  [self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];
  </code></pre>
  
  <p>And in observer method I'll update <code>tableHeightConstraint</code> constraint.</p>
  
  <pre><code>- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
      self.tableHeightConstraint.constant = self.tableView.contentSize.height;
      [self.tableView setNeedsLayout];
      [self.tableView layoutIfNeeded];
  }
  </code></pre>
  
  <p>After trying all possibilities end up here. Warnings for constraints.</p>
  
  <pre><code>(
      "&lt;NSLayoutConstraint:0x7fbeb1e7e8e0 V:|-(0)-[UITableView:0x7fbeb2843000]   (Names: '|':BluePrintView:0x7fbeb1e7ac30 )&gt;",
      "&lt;NSLayoutConstraint:0x7fbeb1e7e700 V:[UITableView:0x7fbeb2843000]-(0)-|   (Names: '|':BluePrintView:0x7fbeb1e7ac30 )&gt;",
      "&lt;NSLayoutConstraint:0x7fbeb1e7e9b0 V:[UITableView:0x7fbeb2843000(480)]&gt;",
      "&lt;NSLayoutConstraint:0x7fbeb1e81a20 '_UITemporaryLayoutHeight' V:[BluePrintView:0x7fbeb1e7ac30(0)]&gt;"
  )
  
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0x7fbeb1e7e700 V:[UITableView:0x7fbeb2843000]-(0)-|   (Names: '|':BluePrintView:0x7fbeb1e7ac30 )&gt;
  </code></pre>
  
  <p>I solved these warnings by removing <code>|</code> line from <code>[tableView]</code> (by removing bottom constraint to superView), but then tableView does not get desire height.</p>
  
  <p>Any idea how to get full tableView height with out any constraint warnings.</p>
  
  <p>P.S. All views are created by code, no XIB no Storyboard layout. iOS 9.2</p>