title: How do I programmatically add a view right under the navigation bar?
body: <p>I'm trying to add a view to a <code>UINavigationController</code> with its top aligned with the navigation bar's bottom. </p>
  
  <p>I tried using constraints by adding the following to my <code>UINavigationController</code> subclass: </p>
  
  <pre><code>override func viewDidAppear(_ animated: Bool) {
             self.label = UILabel()
      self.label?.translatesAutoresizingMaskIntoConstraints = false
      self.label?.backgroundColor = UIColor.red
      self.label?.text = "label text"
      self.view.addSubview(self.label!)
      let horConstraint = NSLayoutConstraint(item: label!, attribute: .top, relatedBy: .equal,
                                             toItem: topLayoutGuide, attribute: .bottom,
                                             multiplier: 1.0, constant: 0.0)
      let widthConstr = NSLayoutConstraint(item: label!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
  
      let heightConstr = NSLayoutConstraint(item: label!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
      view.addConstraints([horConstraint, widthConstr, heightConstr])
  }
  </code></pre>
  
  <p>With this being the result:</p>
  
  <p><a href="https://i.stack.imgur.com/wETPU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wETPU.png" alt="enter image description here"></a></p>
  
  <p>And I tried by setting the frame of my subview:</p>
  
  <pre><code>override func viewDidAppear(_ animated: Bool) {
  
      self.label = UILabel(frame: CGRect(x: 0, y: navigationBar.frame.height, width: 300, height: 100))
      self.label?.translatesAutoresizingMaskIntoConstraints = false
      self.label?.backgroundColor = UIColor.red
      self.label?.text = "label text"
      self.view.addSubview(self.label!)
  
  }
  </code></pre>
  
  <p>And this came out:</p>
  
  <p><a href="https://i.stack.imgur.com/sUhHx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/sUhHx.png" alt="enter image description here"></a></p>
  
  <p>In both cases my label covers part of the navigation bar. How to I fix this?</p>