title: Using autolayout constraints programmatically
body: <p>I'm trying to use autolayout in my iOS app programmatically.
  I have a simple view controller with this init code</p>
  
  <pre class="lang-c prettyprint-override"><code>UIView *innerView = [[UIView alloc] initWithFrame:self.view.bounds];
  
  UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button1 setFrame:CGRectMake(50, 50, 150, 50)];
  [button1 setTitle:@"button1" forState:UIControlStateNormal];
  
  UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button2 setFrame:CGRectMake(250, 50, 150, 50)];
  [button2 setTitle:@"button2" forState:UIControlStateNormal];
  
  [innerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button1][button2(==button1)]|" options:NSLayoutFormatAlignAllBaseline metrics:0 views:NSDictionaryOfVariableBindings(button1, button2)]];
  
  [innerView addSubview:button1];
  [innerView addSubview:button2];
  [self.view addSubview:innerView];
  </code></pre>
  
  <p>But when I'm trying to run this, I'm getting this error:</p>
  
  <pre><code>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
  Unable to interpret '|' character, because the related view doesn't have a superview 
  |[button1][button2(==button1)]| 
            ^'
  </code></pre>
  
  <p>What's wrong with it?</p>
  
  <p>And when I'm trying to remove pipes in <code>constraintsWithVisualFormat</code>, I'm getting this warning:</p>
  
  <pre><code>Unable to simultaneously satisfy constraints.
  Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
  (
      "&lt;NSAutoresizingMaskLayoutConstraint:0x716be10 h=--&amp; v=--&amp; UIRoundedRectButton:0x71631f0.midX == + 325&gt;",
      "&lt;NSAutoresizingMaskLayoutConstraint:0x7169940 h=--&amp; v=--&amp; H:[UIRoundedRectButton:0x715fb80(150)]&gt;",
      "&lt;NSAutoresizingMaskLayoutConstraint:0x7169860 h=--&amp; v=--&amp; UIRoundedRectButton:0x715fb80.midX == + 125&gt;",
      "&lt;NSLayoutConstraint:0x7164cd0 UIRoundedRectButton:0x71631f0.width == UIRoundedRectButton:0x715fb80.width&gt;",
      "&lt;NSLayoutConstraint:0x7164940 H:[UIRoundedRectButton:0x715fb80]-(0)-[UIRoundedRectButton:0x71631f0]&gt;"
  )
  
  Will attempt to recover by breaking constraint 
  &lt;NSLayoutConstraint:0x7164940 H:[UIRoundedRectButton:0x715fb80]-(0)-[UIRoundedRectButton:0x71631f0]&gt;
  
  Break on objc_exception_throw to catch this in the debugger.
  The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in &lt;UIKit/UIView.h&gt; may also be helpful.
  </code></pre>
  
  <p>And as result of this, my constraints have no effect at all.
  What am I doing wrong?</p>