title: iOS - constraints animate in an unexpected way in viewdidload?
body: <p>I'm practicing auto layout and learning about animating constraints.</p>
  
  <p>My first question is.  If I am dynamically adding views it seems cumbersome to dynamically add their constraints to the parent view as well.  Is there any clean way to accomplish a flexible layout where views can be added and removed programmatically?  Or would this mean I should probably think of a simpler solution for what I'm trying to accomplish?</p>
  
  <p>Second question.  I have created two views, and some constraints in code.  I am just trying to resize the height constraint on the first view on load so that it will become shorter, and the second view will shift upwards accordingly.</p>
  
  <p>here is some code:</p>
  
  <pre><code>first = [[UIView alloc]initWithFrame:CGRectZero];
  
  [first setBackgroundColor:[UIColor blueColor]];
  
  [first setTranslatesAutoresizingMaskIntoConstraints:NO];
  
  [self.view addSubview:first];
  
  
  
  UIView *second = [[UIView alloc]initWithFrame:CGRectZero];
  
  [second setBackgroundColor:[UIColor redColor]];
  
  [second setTranslatesAutoresizingMaskIntoConstraints:NO];
  
  [self.view addSubview:second];
  
  
  
  NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
  
  NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
  
  
  top = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:40];
  
  height = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:80];
  
  [self.view addConstraints:@[leading,trailing,top,height]];
  
  [height setConstant:10];
  
  [UIView animateWithDuration:0.5 animations:^{
  
      [self.view layoutIfNeeded];
  
  }];
  </code></pre>
  
  <p>okay so at the bottom here I run my animation.. my second view is already positioned where it would be at the end of the animation.  the first view expands from its top left corner, to its bottom right corner.  it animates diagonally and ends up with a height of 10.</p>
  
  <p>Can anyone explain this behavior.  I noticed if I assign the constraints, and make them animate on an IBAction (button touch) then it will animate as expected.</p>