title: How can I add a navigation bar programmatically that actually fits the screen?
body: <p>I am using <a href="https://github.com/xmartlabs/Eureka" rel="nofollow noreferrer">Eureka forms</a>. One of my custom selector rows presents another VC (<code>EditorVC</code>) modally, in the style of <code>formSheet</code>.</p>
  
  <p>I can do this by presenting a <code>UINavigationController</code> and have <code>EditorVC</code> as the root VC. However, that would require me to pass data (namely the row's value when something changes in <code>EditorVC</code>) between the <code>UINavigationController</code> and <code>EditorVC</code>. That's too troublesome for me.</p>
  
  <p>This is why I resorted to programmatically creating a nav bar.  </p>
  
  <p>I searched on SO and this is my first try:      </p>
  
  <pre><code>myNav = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.width, height: 44))
  myNav.barTintColor = #colorLiteral(red: 0.3529411765, green: 0.7333333333, blue: 0.3529411765, alpha: 1)
  myNav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
  self.view.addSubview(myNav)
  let cancelItem: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel))
  cancelItem.tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
  let doneItem: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
  doneItem.tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
  let navigItem: UINavigationItem = UINavigationItem(title: "Title")
  navigItem.rightBarButtonItem = doneItem
  navigItem.leftBarButtonItem = cancelItem
  myNav.items = [navigItem]
  </code></pre>
  
  <p>When I run the app on an iPhone, the nav bar shows up as expected, but if I change the screen orientation, the nav bar maintains its width and thus does not cover the full width of the screen. On an iPad, the nav bar is too wide. It exceeds the width of the form, so the right bar button item can't be seen. The title is also not centered.</p>
  
  <p>So I tried to add layout constraints to the nav bar:</p>
  
  <pre><code>let top = NSLayoutConstraint(item: myNav, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0)
  let leading = NSLayoutConstraint(item: myNav, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
  let trailing = NSLayoutConstraint(item: myNav, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
  view.addConstraints([top, leading, trailing])
  </code></pre>
  
  <p>But apparently it does not change anything. When I run the app, the exact same thing happens.</p>
  
  <p>How can I create a nav bar that can always fit the screen width? This must be possible, right? Because the <code>UINavigationController</code> somehow did it.</p>