title: Swift 3 - Create title bar constraints programmatically
body: <p>I am using Swift 3, iOS 10, XCode 8.2.</p>
  
  <p>In my code, I need to create a <code>UIViewController</code> programmatically and hence, specify its layout and content programmatically as well.</p>
  
  <pre><code>@IBAction func testViewController() {
  
      let detailViewController = UIViewController()
      detailViewController.view.backgroundColor = UIColor.white
  
      let titleLabel: UILabel = {
          let label = UILabel()
          label.translatesAutoresizingMaskIntoConstraints =  false
          label.backgroundColor = UIColor.blue
          label.text = "Scan Results"
          label.textAlignment = .center
          label.font = UIFont.boldSystemFont(ofSize: 18)
          label.textColor = UIColor.white
          return label
      }()
  
      let titleConstraints: [NSLayoutConstraint] = [
          NSLayoutConstraint(item: titleLabel, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0),
          NSLayoutConstraint(item: titleLabel, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0),
          NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0),
          NSLayoutConstraint(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40)
      ]
  
      detailViewController.view.addSubview(titleLabel)
  
      detailViewController.view.addConstraints(titleConstraints)
  
      self.navigationController?.pushViewController(detailViewController, animated: true)
  }
  </code></pre>
  
  <p>In the vertical view (ignore all the other junk; just focus on the blue title bar): </p>
  
  <p><a href="https://i.stack.imgur.com/QNeBz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QNeBz.png" alt="enter image description here"></a></p>
  
  <p>But in the horizontal view: </p>
  
  <p><a href="https://i.stack.imgur.com/Gsm99.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gsm99.png" alt="enter image description here"></a></p>
  
  <p>What is the correct constraint to set so that it takes up the entire width of the bar and there isn't that extra space from the top since the status bar disappears when horizontal? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After making @thexande suggestions, I do get an error: </p>
  
  <blockquote>
    <p>[LayoutConstraints] The view hierarchy is not prepared for the
    constraint: <code>&lt;NSLayoutConstraint:0x608000098100
    UILabel:0x7fe35b60edc0'Scan Results'.left ==
    UIView:0x7fe35b405c20.left   (inactive)&gt;</code>     When added to a view, the
    constraint's items must be descendants of that view (or the view
    itself). This will crash if the constraint needs to be resolved before
    the view hierarchy is assembled. Break on
    -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug. 2017-02-24 21:01:59.807 EOB-Reader[78109:10751346] <strong>* Assertion failure in
    -[UIView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:],
    /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/NSLayoutConstraint_UIKitAdditions.m:649
    2017-02-24 21:01:59.951 EOB-Reader[78109:10751346] *</strong> Terminating app
    due to uncaught exception 'NSInternalInconsistencyException', reason:
    'Impossible to set up layout with view hierarchy unprepared for
    constraint.'</p>
  </blockquote>
  
  <p>I've also updated my code in the original post.</p>