title: Programmatically creating scrollview-contentview-views, contentview not appearing
body: <p>I am trying to programmatically create a scrollview, which allows users to scroll vertically when switching their phones to landscape mode, but I have had no luck so far :( (I have tried to make this work in numerous ways, but it just did not work, and searching did not help at all)</p>
  
  <p>What I want:
  I want to create a scrollview that allows me to keep the views inside the scrollview nicely positioned across the view, without stacking up on one another. </p>
  
  <p><strong>Major attempts</strong></p>
  
  <p>1st attempt:
  I created a scrollview, which has anchors attached to the superview's(self.view) respective anchors, and I added a custom button that has top, left, right anchors attached to the scrollview's respective anchors (top, left, right). Then I implemented viewWillTransitionToSize method to change scrollView's contentsize to make sure scrollview maintained the height of the longer side of the screen. Scrolling worked as expected, but subviews of the scrollview did not appear properly. I am thinking this is possibly because we are not encouraged to add multiple views to the same scrollview (<strong>can anyone explain why this is so?</strong>).</p>
  
  <p>2nd attempt:
  I created contentview inside scrollview to contain all the views I might want to add to the scrollview, but still, this contentview did not appear. </p>
  
  <p>Below is the code I wrote so far</p>
  
  <pre><code>let scrollView: UIScrollView = {
      let scrollView = UIScrollView()
      scrollView.contentSize = UIScreen.mainScreen().bounds.size
      scrollView.backgroundColor = UIColor.blueColor()
      scrollView.translatesAutoresizingMaskIntoConstraints = false
      return scrollView
  }()
  let contentView: UIView = {
      let view = UIView()
      view.backgroundColor = UIColor.yellowColor()
      view.translatesAutoresizingMaskIntoConstraints = false
      return view
  }()
  let loginButton: UIButton = {
      let button = UIButton()
      button.backgroundColor = UIColor.blackColor()
      button.setTitle("LOGIN", forState: .Normal)
      button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
      button.layer.cornerRadius = 5
      button.layer.masksToBounds = true
      button.translatesAutoresizingMaskIntoConstraints = false
      return button
  }()
  override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
      // To portrait
      if(size.width &lt; size.height){
          self.scrollView.contentSize = CGSize(width: size.width, height: size.height)
      }// To Landscape
      else{
          self.scrollView.contentSize = CGSize(width: size.width, height: size.width)
      }
  }
  func setupViews(){
      self.view.addSubview(self.scrollView)
      self.scrollView.addSubview(self.contentView)
      self.contentView.addSubview(self.loginButton)
  }
  func setupConstraints(){
      self.scrollView.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
      self.scrollView.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
      self.scrollView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor).active = true
      self.scrollView.rightAnchor.constraintEqualToAnchor(self.view.rightAnchor).active = true
  
      self.contentView.topAnchor.constraintEqualToAnchor(self.scrollView.topAnchor).active = true
      self.contentView.bottomAnchor.constraintEqualToAnchor(self.scrollView.bottomAnchor).active = true
      self.contentView.leftAnchor.constraintEqualToAnchor(self.scrollView.leftAnchor).active = true
      self.contentView.rightAnchor.constraintEqualToAnchor(self.scrollView.rightAnchor).active = true
  
      self.loginButton.bottomAnchor.constraintEqualToAnchor(self.contentView.bottomAnchor).active = true
      self.loginButton.leftAnchor.constraintEqualToAnchor(self.contentView.leftAnchor).active = true
      self.loginButton.rightAnchor.constraintEqualToAnchor(self.contentView.rightAnchor).active = true
      self.loginButton.heightAnchor.constraintEqualToConstant(50).active = true
  
  }
  override func viewDidLoad() {
      super.viewDidLoad()
  
      self.setupViews()
      self.setupConstraints()
  }
  </code></pre>
  
  <p>EDIT: Here is the screenshot of the phone screen when I run simulator with the code above.
  <a href="https://i.stack.imgur.com/OYqhX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OYqhX.png" alt="Description here"></a></p>
  
  <p>Blue screen is the scrollview, and currently contentview in yellowcolor is not appearing on top of the scrollview.</p>