title: Presenting a popup view controller programatically using auto layout
body: <p>I am building my UI from code and need to present another view controller as a popover, over the current controller. The popover will act as a modal dialog and will use about 50% of the screen size. The contents of the popover must be constrained in the screen center and support device rotation. </p>
  
  <p>My initial implementation was done in viewDidLoad() of the PopoverController class and it worked as expected. However, I do not want view code in my controllers. After moving the popover view to a separate file, the fun began; auto layout and device rotation no longer worked as expected. </p>
  
  <p>I have made a basic app showcasing this. The main controller has a button for displaying the popover. The popover has yellow background and should display a blue view in the center.</p>
  
  <p><strong>MainViewController :: Test button for displaying the popover</strong></p>
  
  <pre><code>@objc func boxButtonTapped() {
          let vc = PopoverController()
          vc.modalPresentationStyle = .overCurrentContext
          vc.modalTransitionStyle = .crossDissolve
          present(vc, animated: true, completion: nil)
      }
  </code></pre>
  
  <p><strong>Popover Controller</strong></p>
  
  <pre><code>class PopoverController: UIViewController {
  
      var popoverView = PopoverView()
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
      override func loadView() {
          view = popoverView
      }
  }
  </code></pre>
  
  <p><strong>Popover View</strong></p>
  
  <pre><code>class PopoverView: UIView {
  
      let blueBox: UIView = {
          let bb = UIView()
          bb.backgroundColor = .blue
          //bb.frame.size = CGSize(width: 100, height: 100)
          bb.translatesAutoresizingMaskIntoConstraints = false
          return bb
      }()
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          createSubViews()
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          createSubViews()
      }
  
      func createSubViews() {
          translatesAutoresizingMaskIntoConstraints = false
          backgroundColor = .yellow
  
          addSubview(blueBox)
  
          blueBox.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
          blueBox.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
          blueBox.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.7).isActive = true
          blueBox.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.4).isActive = true
      }
  }
  </code></pre>
  
  <ol>
  <li><p>When running the above code, the popover doesn`t even display! This is my first question, can anyone see why?</p></li>
  <li><p>If I change translatesAutoresizingMaskIntoConstraints = false to true. Then the popover displays, but it does not resize when the device rotates. I do not know why and I don´t understand why translatesAutoresizingMaskIntoConstraints needs to be set to true in order for the view to display in the first place.</p></li>
  </ol>
  
  <p>There must be a simple solution for such a trivial task. I have tried so many different approaches and listing them here would be impossible. I am new to IOS dev and Swift and I really would like to get a good start with clean code. Thank you for your time!</p>
  
  <p><strong>Update</strong>
  I published the sample project on GitHub. So if you have the time and want  a challenge/prove a concept then here it is: <a href="https://github.com/igunther/CleanController" rel="nofollow noreferrer">https://github.com/igunther/CleanController</a></p>