title: iOS - Position UIView in center of superview using Auto Layout programmatically
body: <p>How do you programmatically set a UIView to be in the center of its superview using Auto Layout? </p>
  
  <pre><code>UIButton* viewObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [viewObj setTranslatesAutoresizingMaskIntoConstraints:NO];
  [viewObj setBackgroundColor:[UIColor greenColor]];
  [self.view addSubview:viewObj];
  
  NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:viewObj
                                                        attribute:NSLayoutAttributeCenterX
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.view
                                                        attribute:NSLayoutAttributeCenterX 
                                                       multiplier:1.0
                                                         constant:0];
  [self.view addConstraint:cn];
  cn = [NSLayoutConstraint constraintWithItem:viewObj 
                                    attribute:NSLayoutAttributeCenterY
                                    relatedBy:NSLayoutRelationEqual 
                                       toItem:self.view
                                    attribute:NSLayoutAttributeCenterY
                                   multiplier:1.0
                                     constant:0];
  [self.view addConstraint:cn];
  </code></pre>
  
  <p>The above code works for me for UIButton, but I'm having trouble replacing the first line with something that works for a UIView. </p>
  
  <p>I've tried</p>
  
  <pre><code>UIView* viewObj = [UIView alloc] initWithFrame:CGRectZero];
  </code></pre>
  
  <p>but the view does not show up in simulator. </p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks!</p>