title: AutoLayout, Constraints and Animation
body: <p>Let's assume that I have this <code>UIView</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/6SwF4.png" alt="enter image description here"></p>
  
  <p>with these relative constraints:</p>
  
  <pre><code>@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftMarginConstraint;
  @property (strong, nonatomic) IBOutlet NSLayoutConstraint *topMarginConstraint;
  @property (strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
  @property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
  </code></pre>
  
  <p>Okay now let's assume that when the user click on that <code>UIButton</code> the button should move to the bottom-right corner of the view. We can easily use two constraints defining bottom space between the button and the bottom layout guide and right space (trailing space) from the button and the right edge of the view.</p>
  
  <p>The problem it's that the <code>UIButton</code> already had two constraints (left/top) AND two contraints defining its width and its height so we can't add two new constraints because they would conflict with the other ones.</p>
  
  <p>Simple and common situation for an animation scenario, but it causing me some problems. Ideas?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When the user taps on the <code>UIButton</code>, I need that the button:</p>
  
  <ol>
  <li>change its title to "Second"</li>
  <li>wait 1 second and move to the bottom-right corner (remove top and left margin constraints and add bottom and right margin constraints)</li>
  <li>change its title to "Third"</li>
  <li>wait 1 second and move to the top-right corner (remove bottom margin constraint
  and add top margin constraint)</li>
  </ol>
  
  <p>Am I seriously bound to use this messy code?</p>
  
  <pre><code>@implementation ViewController
  {
      NSLayoutConstraint *_topMarginConstraint;
      NSLayoutConstraint *_leftMarginConstraint;
      NSLayoutConstraint *_bottomMarginConstraint;
      NSLayoutConstraint *_rightMarginConstraint;
  }
  
  - (IBAction)buttonPressed:(id)sender
  {
      UIButton *button = sender;
  
      // 1.
      [sender setTitle:@"Second" forState:UIControlStateNormal];
  
      // 2.
      double delayInSeconds = 1.0;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
          [button removeConstraints:@[self.leftMarginConstraint, self.topMarginConstraint]];
          _bottomMarginConstraint = [NSLayoutConstraint constraintWithItem:self.view
                                                                 attribute:NSLayoutAttributeBottom
                                                                 relatedBy:0 toItem:button
                                                                 attribute:NSLayoutAttributeBottom
                                                                multiplier:1
                                                                  constant:20];
          [self.view addConstraint:_bottomMarginConstraint];
  
          _rightMarginConstraint = [NSLayoutConstraint constraintWithItem:self.view
                                                                attribute:NSLayoutAttributeRight
                                                                relatedBy:0 toItem:button
                                                                attribute:NSLayoutAttributeRight
                                                               multiplier:1
                                                                 constant:20];
          [self.view addConstraint:_rightMarginConstraint];
  
          [UIView animateWithDuration:1 animations:^{
              [self.view layoutIfNeeded];
          } completion:^(BOOL finished) {
              // 3.
              [sender setTitle:@"Third" forState:UIControlStateNormal];
  
              // 4.
              double delayInSeconds = 1.0;
              dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
              dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
                  [button removeConstraint:_bottomMarginConstraint];
                  _topMarginConstraint = [NSLayoutConstraint constraintWithItem:self.view
                                                                         attribute:NSLayoutAttributeTop
                                                                         relatedBy:0 toItem:button
                                                                         attribute:NSLayoutAttributeTop
                                                                        multiplier:1
                                                                          constant:20];
                  [UIView animateWithDuration:1 animations:^{
                      [self.view layoutIfNeeded];
                  }];
              });
          }];
      });
  }
  </code></pre>
  
  <p>Serious? :D</p>