title: Updating multiple view constraints at the same time
body: <p>Is there a way to tell the auto layout engine, at runtime, that I'm going to change multiple constraints on a view at the same time? I keep running into a situation where the order in which I update the constraints matters because even though when all is said and done all of the constraint conditions are satisfied, changing the constant on one before the other throws a LAYOUT_CONSTRAINTS_NOT_SATISFIABLE exception (or whatever the exception is actually called...)</p>
  
  <p>Here's an example. I create and add a view to my current view and set some constraints on it. I'm saving the leading and trailing edge constraints so I can change their constants later.</p>
  
  <pre><code>- (MyView*)createMyView
  {
      MyView* myView = [[MyView alloc init];
  
      [myView setTranslatesAutoresizingMaskIntoConstraints:NO];
      [self.mainView addSubview:myView];
  
      NSDictionary* views = @{@"myView" : myView};
      NSLayoutConstraint* leading = [NSLayoutConstraint constraintWithItem:myView
                                                                 attribute:NSLayoutAttributeLeading
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.view
                                                                 attribute:NSLayoutAttributeLeading
                                                                multiplier:1.0f
                                                                  constant:0];
      NSLayoutConstraint* trailing = [NSLayoutConstraint constraintWithItem:myView
                                                                  attribute:NSLayoutAttributeTrailing
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeTrailing
                                                                 multiplier:1.0f
                                                                   constant:0];
      myView.leadingConstraint = leading;
      myView.trailingConstraint = trailing;
  
      [self.view addConstraints:@[leading, trailing]];
  
      NSString* vflConstraints = @"V:|[myView]|";
      [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflConstraints
                                                                        options:NSLayoutFormatAlignAllCenterX
                                                                        metrics:nil
                                                                          views:views]];
      return myView;
  }
  </code></pre>
  
  <p>When swipe gesture on the view fires off, I create a new view off to one side or the other depending on the direction of the swipe. I then update the constraints and animate so the new incoming view "pushes" the old view out of view.</p>
  
  <pre><code>- (void)transitionToCameraView:(MyView*)newCameraView
                  swipeDirection:(UISwipeGestureRecognizerDirection)swipeDirection
  {
      // Set new view off screen before adding to parent view
      float width = self.myView.frame.size.width;
      float height = self.myView.frame.size.height;
      float initialX = (swipeDirection == UISwipeGestureRecognizerDirectionLeft) ? width : -width;
      float finalX = (swipeDirection == UISwipeGestureRecognizerDirectionLeft) ? -width : width;
      float y = 0;
  
      [newView setFrame:CGRectMake(initialX, y, width, height)];
      [self.mainView addSubview:newView];
  
      self.myView.leadingConstraint.constant = finalX;
      self.myView.trailingConstraint.constant = finalX;
  
      // Slide old view out and new view in
      [UIView animateWithDuration:0.4f
                            delay:0.0f
                          options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                       animations:^
              {
                  [newView setFrame:self.myView.frame];
                  [self.myView layoutIfNeeded];
              }
                       completion:^(BOOL finished)
              {
                  [self.myView removeFromSuperview];
                  self.myView = newView;
              }];
  }
  </code></pre>
  
  <p>This works fine when the swipe direction is UISwipeGestureRecognizerDirectionLeft, but when it is UISwipeGestureRecognizerDirectionRight, the exception gets thrown at</p>
  
  <pre><code>    self.myView.leadingConstraint.constant = finalX;
  </code></pre>
  
  <p>If I check the direction and update the constraints in the reverse order, everything is fine and the exception does not get thrown:</p>
  
  <pre><code>if(swipeDirection == UISwipeGestureRecognizerDirectionLeft)
  {
      self.myView.leadingConstraint.constant = finalX;
      self.myView.trailingConstraint.constant = finalX;
  }
  else
  {
      self.myView.trailingConstraint.constant = finalX;
      self.myView.leadingConstraint.constant = finalX;
  }
  </code></pre>
  
  <p>It DOES make sense to me WHY the exception is getting thrown, but it seems like changing multiple constraint parameters is something we should be able to do and let the auto layout engine know that we're going to be doing that, instead of it immediately trying to satisfy the constraint and throwing up.</p>
  
  <p>Is there a way to tell the auto layout engine that I'm making changes to multiple constraints and then tell it to run again when I'm done, or am I stuck doing it this way? It seems really silly to me to have the order in which I update the constraints matter here when everything is actually going to be fine when I'm finished with the changes.</p>
  
  <p><strong>EDIT</strong><br>
  Upon further investigation, I've discovered that the underlying reason WHY the order matters is that myView has a subview added to it that has a constraint specifying that it's leading edge is 10pt from myView's leading edge. If I make and adjustment to the new constant to account for this, there is no longer a constraint conflict and there is no exception. </p>
  
  <pre><code>self.myView.leadingConstraint.constant = finalX+25; // Just arbitrary number that happened to work
  self.myView.trailingConstraint.constant = finalX;
  </code></pre>
  
  <p>The reason there is a problem is that the new constraint when swiping left-to-right collapses myView to have a width of 0. Obviously there is not enough room in a view of width 0 to add a 10pt leading edge constraint to a subview. Changing the constants in the reverse order, instead, first widens the view by increasing the trailing edge, then shrinks it back up again by decreasing the leading edge.</p>
  
  <p>Even more reason to have a way to tell the auto layout system that we're going to make multiple changes.</p>