title: Autolayout & programmatic constraints: How to deal with updateConstraints firing multiple times?
body: <p>When programmatically creating layouts, I follow Apple's advice: override -updateConstraints, add custom constraints, and call -setNeedsUpdateConstraints once subviews have been added to the view. My typical setup looks like this:</p>
  
  <pre><code>- (void)setupViews
  {
      //Style View
  
      //Add gesture recognizers
  
      //Add Subviews
  
  
      [self setNeedsUpdateConstraints];
  }
  
  - (void)updateConstraints
  {
      //Add custom constraints       
  
      [super updateConstraints];
  }
  </code></pre>
  
  <p><strong>The problem</strong></p>
  
  <p>There are occasions when -updateConstraints gets fired multiple times (for example, when a view's controller is presented or pushed w/ animation). The problem here is that each constraint added gets re-added. This becomes a serious problem when trying to on-demand change the constant of an added constraint, since there are two of the original constraint that subsequently conflict with each other. I imagine that even when you aren't manipulating the constraints after creating them, having double what you doesn't seem good.</p>
  
  <p><strong>Potential solutions</strong></p>
  
  <p>1 - Remove all constraints effecting the view before applying them in -updateConstraints:</p>
  
  <pre><code> - (void)updateConstraints
      {   
          //Remove all constraints affecting view &amp; subviews
  
          //Add custom constraints
  
          [super updateConstraints];       
      }
  </code></pre>
  
  <p>2 - Set a layout flag &amp; check against it before adding custom constraints:</p>
  
  <pre><code>- (void)updateConstraints
  {
      if (self.didAddConstraints) {
          [super updateConstraints];
          return;
      }
  
      //Add custom constraints   
  
      self.didAddConstraints = YES;    
  
      [super updateConstraints];
  }
  </code></pre>
  
  <p>3 - Don't worry about doubling up on constraints, and whenever changing a constant is needed, only remove that constraint before re-adding.</p>
  
  <p>3 - Something awesome that I haven't thought of.</p>
  
  <hr>
  
  <p>What's the best practice here? </p>