title: Auto-Layout Issues: iOS 7 vs iOS8
body: <p>I am trying to create a collapsible toolbar that works like this (running in iOS 7 -- ugly colors etc. for visualization purposes):</p>
  
  <p><img src="https://i.stack.imgur.com/01kRk.gif" alt="Toolbar behavior in iOS 7"></p>
  
  <p>However, when I run the code in iOS 8, this is what happens:
  <img src="https://i.stack.imgur.com/tkMU2.gif" alt="enter image description here">
  I have set up a constraint system that is based on the following:
  <img src="https://i.stack.imgur.com/nrglr.jpg" alt="collapsible toolbar diagram"></p>
  
  <p>A <code>centering view</code> (not shown) keeps the toolbar in the screen middle.
  A <code>sizing view</code> is adjusted to collapse the toolbar. The <code>sizing view</code> is anchored to the right of the <code>centering view</code>(via a <code>trailing</code> constraint).
  A <code>container view</code> holds the actual content of the toolbar. It is anchored to the right of the <code>sizing view</code> (also via a <code>trailing</code> constraint).
  Various <code>content views</code> are contained in the <code>container view</code>. They have no constraints. The default constraints applied by the system should be width, height, top, left, which ensures that they keep their relative positions in the <code>container view</code>.</p>
  
  <p>The collapsing of the toolbar is achieved as follows:</p>
  
  <pre><code>- (IBAction)showLess:(id)sender {
      self.widthConstraint.constant = 50; // adjust this number for collapse / expand
      [UIView animateWithDuration:0.3 animations:^{
          [self.centeringView layoutIfNeeded]; // trigger animation
      }];
  }
  </code></pre>
  
  <p>Which adjusts the width of the <code>sizing view</code>.</p>
  
  <p><strong>Problem:</strong>
  iOS 8 seems to behave as if I had left anchored the content view, but this is not true.</p>
  
  <p>I would sincerely appreciate:</p>
  
  <ul>
  <li>An explanation as to why iOS 8 would have such a radically different interpretation of the given (reasonably simple) constraints.</li>
  <li>A pointer as to how I can get the intended behavior in iOS 8</li>
  </ul>
  
  <p><a href="https://www.dropbox.com/s/4j7ut7mns5wm6bn/AutoLayoutIssueStackOverFlow.zip?dl=0" rel="nofollow noreferrer">Source code</a> available <a href="https://www.dropbox.com/s/4j7ut7mns5wm6bn/AutoLayoutIssueStackOverFlow.zip?dl=0" rel="nofollow noreferrer">here</a> (updated version that works in iOS 8).</p>
  
  <p><strong>UPDATE:</strong>
  The issue was solved with answers from Stack-overflow. Basically, the right answer is <a href="https://stackoverflow.com/a/26066992/228342">this</a>, but it was nicely summarized in <a href="https://stackoverflow.com/a/26070049/228342">this answer</a>.
  The difference between iOS7 and iOS8 is not in the way the constraints are interpreted, but in the way that update commands are trickled down through the view hierarchy. 
  When I implemented the behavior first in iOS 7, I noticed that the animation would only work properly if I called <code>layoutIfNeeded</code> on the parent view of the <code>sizing view</code> (i.e. on <code>centering view</code>). In iOS 7 this apparently  trickled down the view hierarchy automatically. In iOS 8, this is not the case. You have to manually invalidate the view whose constraints have changed with <code>setNeedsLayout</code>, and then update the layout with <code>layoutIfNeeded</code>. My solution in the updated code looks like this:</p>
  
  <pre><code>- (IBAction)showLess:(id)sender {
      self.widthConstraint.constant = 50;
      [self.sizingView setNeedsLayout]; // *** THIS LINE IS NECESSARY TO MAKE THINGS WORK IN iOS 8
      [UIView animateWithDuration:0.3 animations:^{
          [self.sizingView layoutIfNeeded]; // trigger animation
      }];
  }
  </code></pre>
  
  <p>I hope this helps others who are also stuck on this forward compatibility issue.</p>