title: Adding and setting NSLayoutConstraint via code
body: <p>I have some trouble with <code>NSLayoutConstraint</code>.</p>
  
  <p>NSLayoutConstraint gives me an exception if I try to alter <code>constant</code> using the <code>setConstant:</code> method. I only have this problem when I add the height constraint via code.</p>
  
  <p>First off, I'm getting the height constraint like so:</p>
  
  <pre><code>NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeWidth];
  NSArray *filteredArray = [[self constraints] filteredArrayUsingPredicate:predicate];
  return filteredArray[0];
  </code></pre>
  
  <p>Which gives me the right constraint. I have a NSTextField subclass where this works perfectly. The constraints are set in the Interface Builder, and I can set and alter the constant.</p>
  
  <p>Now I have a view, where I add different subviews at run-time. 
  Those subviews are located in own NIBs, which means I can't pin their width and height.
  So I thought I'd add the constraints as soon as the view is being added to a superview.
  This is being executed in <code>viewDidMoveToSuperview</code>.</p>
  
  <pre><code>// Pin Width &amp; Height
  [self addConstraint:[NSLayoutConstraint constraintWithItem:self
                               attribute:NSLayoutAttributeWidth
                               relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                               attribute:NSLayoutAttributeNotAnAttribute
                              multiplier:1.0f
                                constant:self.frame.size.width]];
  [self addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                   attribute:NSLayoutAttributeHeight
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:nil
                                                   attribute:NSLayoutAttributeNotAnAttribute
                                                  multiplier:1.0f
                                                    constant:self.frame.size.height]];
  </code></pre>
  
  <p>The constraints are added, I can confirm this with an NSLog.</p>
  
  <pre><code>"&lt;NSLayoutConstraint:0x100605cc0 H:[ITControlPanelChildView:0x100616eb0(269)]&gt;",
  "&lt;NSLayoutConstraint:0x100614410 V:[ITControlPanelChildView:0x100616eb0(317)]&gt;"
  </code></pre>
  
  <p>Now, finally, when I try to alter the constraint using <code>[constraint setConstant:somethingDifferent];</code> I get the following exception:</p>
  
  <pre><code>Unable to simultaneously satisfy constraints:
  (
      "&lt;NSLayoutConstraint:0x10192fcb0 V:[ITControlPanelChildView:0x10192e4f0(100)]&gt;",
      "&lt;NSAutoresizingMaskLayoutConstraint:0x10053ff10 h=--&amp; v=&amp;-- V:[ITControlPanelChildView:0x10192e4f0(317)]&gt;"
  )
  
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0x10192fcb0 V:[ITControlPanelChildView:0x10192e4f0(100)]&gt;
  </code></pre>
  
  <p>This is exactly the constraint I'm trying to change.
  Can anyone explain to me why this is happening?</p>
  
  <hr>
  
  <h1>EDIT</h1>
  
  <p>I just read that the <code>NSAutoresizingMaskLayoutConstraint</code>s are added automatically, which you can disable if you set <code>[self setTranslatesAutoresizingMaskIntoConstraints:NO];</code>.</p>
  
  <p>If I disable it, it works.</p>
  
  <p>Even better would be if I could access the <code>NSAutoresizingMaskLayoutConstraint</code> which are created. Does anyone know how this works?</p>