title: Setting Constraints programmatically different from setting them in IB?
body: <p>I am new to Auto Layout in iOS. I really like the concept in principle, but it's driving me nuts trying to get the simplest things done. I suspect I'm still missing some simple underlying principle. I am trying to learn by doing and get the basics right before actually working with it in an app, so I'm creating very simple test projects. Here's one as simple as it gets that doesn't work as expected. First the part that works. In IB, I add a View to fill the entire viewcontroller, and XCode automatically sets the constraints to Top/Bottom/Leading/Trailing and the Space to 0. When done with IB, it works as expected:</p>
  
  <p><img src="https://i.stack.imgur.com/9Uydm.png" alt="enter image description here"> </p>
  
  <p>rotates to</p>
  
  <p><img src="https://i.stack.imgur.com/osQhR.png" alt="enter image description here"></p>
  
  <p>Great!</p>
  
  <p>Now I am trying to do the same thing in code:</p>
  
  <pre><code>- (void)viewDidLoad
  {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  UIView *redView = [[UIView alloc] initWithFrame:self.view.bounds];
  redView.backgroundColor = [UIColor redColor];
  [self.view addSubview:redView];
  
  redView.translatesAutoresizingMaskIntoConstraints = NO;
  
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
  }
  </code></pre>
  
  <p>That's all the code there is other than the default code for a Single-view application. </p>
  
  <p>When I run the above code, trying to mirror the same thing as I get with IB programmatically, I get this after rotation:</p>
  
  <p><img src="https://i.stack.imgur.com/xh2AR.png" alt="enter image description here"></p>
  
  <p>How come that the same constraints lead to different results? It's probably something really simple and embarrassingly stupid that I am missing. Help!!!</p>