title: Set UIViewController view property to custom UIView class without storyboard / nib
body: <p>I have a <code>UIViewController</code> called <code>LoginViewController</code>. I want to build the view of that <code>LoginViewController</code> <strong>fully programmatically</strong> in a custom <code>UIView</code> class called <code>LoginView</code> instead of building all the elements within my <code>LoginViewController</code>. This way I'm preventing "View" code in a Controller class (MVC). </p>
  
  <p>In the code below I'm setting the view of my <code>LoginViewController</code> to my <code>LoginView</code> which for simplicity only contains 2 <code>UILabels</code></p>
  
  <pre><code>class LoginViewController: UIViewController {
  
  override func loadView() {
      super.loadView()
  
      self.view = LoginView(frame: CGRect.zero)
  }
  </code></pre>
  
  <p>The LoginView class initialises both labels and should set some constraints.</p>
  
  <pre><code>class LoginView: UIView {
  
  var usernameLabel: UILabel!
  var passwordLabel: UILabel!
  
  
  override init (frame : CGRect) {
      super.init(frame : frame)
  
      setupLabels()
  }
  
  convenience init () {
      self.init(frame:CGRect.zero)
  }
  
  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
  
  private func setupLabels(){
      //Init labels and set a simple text
      self.usernameLabel = UILabel()
      self.usernameLabel.text = "Username"
      self.passwordLabel = UILabel()
      self.passwordLabel.text = "Password"
  
      //Set constraints which aren't possible since there is no contentView, perhaps using the frame?
      }
  }
  </code></pre>
  
  <p>This doesn't work since the view's bounds are 0. However I couldn't find any resource that gives insight in whether this is possible, so I tried my approach which didn't work. </p>
  
  <p>How you set the view of a UIViewController to a custom UIView which is made programmatically? Or is the above snippet recommended?</p>
  
  <blockquote>
    <p><strong>This is the working solution based on Jadar's answer:</strong></p>
  
  <pre><code>class LoginViewController: UIViewController {
  
      override func loadView() {
          view = LoginView()
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()        
      //  Do any additional setup after loading the view.
  
      }
  }
  
  class LoginView: UIView {
  
  var usernameLabel: UILabel!
  var passwordLabel: UILabel!
  
  override init(frame: CGRect) {
      super.init(frame: frame)
  
      self.usernameLabel = UILabel()
      self.usernameLabel.text = "Username"
      self.passwordLabel = UILabel()
      self.passwordLabel.text = "Password"
  
      addSubview(usernameLabel)
      addSubview(passwordLabel)
  
      if let superview = usernameLabel.superview{
          //Setting AutoLayout using SnapKit framework
          usernameLabel.snp.makeConstraints { (make) in
              make.center.equalTo(superview)
          }
      }
  }
  </code></pre>
  </blockquote>
  
  <p>Result:</p>
  
  <p><a href="https://i.stack.imgur.com/mnUuY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mnUuY.png" alt="Result"></a></p>