title: Create a vertical UIScrollView programmatically in Swift
body: <p>I've been looking for days for a (working) tutorial or even an example app that uses UIScrollView to scroll vertically, programatically that is. There's tons of tutorials on using storyboard, leaving me at a loss.</p>
  <p>I looked through apple's documentation, and their &quot;guide&quot; still not having a solid example or hint as to where to start. What I've  attempted so far, is doing some of the following.</p>
  <p><strong>Making my view a scrollview Directly in the class</strong></p>
  <pre><code>let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
  </code></pre>
  <p><strong>Then assigning it to the view in my viewDidLoad function</strong></p>
  <pre><code>self.view = scollView
  </code></pre>
  <p><strong>Attempting to change the content size.</strong></p>
  <pre><code>self.scrollView.contentSize = CGSize(width:2000, height: 5678)
  </code></pre>
  <p><strong>Trying to enable scrolling with</strong></p>
  <pre><code>scrollView.scrollEnabled = true
  </code></pre>
  <p><strong>And the last suggestion I could find on doing this programatically</strong></p>
  <pre><code>override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      scrollView.frame = view.bounds
  }
  </code></pre>
  <p>Currently I havn't tried to start adding my objects to the scrollview, (I don't need to zoom, just do vertical scrolling), but I havn't managed to get anything working whatsoever :( In fact, running the app with those additions simply causes UIProblems, The screen is moved up weirdly, and it doesn't fit the entire width of my screen? I tried fixing that making the frame bounds equal to the width, but still didn't work</p>
  <p>I'm not getting any errors.</p>
  <p>I would love to see an example <code>viewcontroller</code> that you can scroll vertically in! Or any help would be hugely appreciated!</p>
  <p>Here is a screenshot of the disaster, attempting to make my view scrollable causes.</p>
  <p><img src="https://i.stack.imgur.com/gQ0SC.png" alt="Screen Shot" /></p>
  <p>(I made the scrollview background red, to see if it was even showing up correctly. Which it seems to be. Yet I can't scroll anywhere</p>
  <p>As suggested in the comments, instead of doing self.view = self.scrollview, I tried adding scrollview as a subview of self.view instead, but with no positive results.</p>
  <p>Adding</p>
  <pre><code>scrollView.contentSize = CGSize(width:2000, height: 5678)
  </code></pre>
  <p>to viewDidLayoutSubviews, as suggested in the comments below made my view scrollable!</p>
  <p>However my layout still looks like a complete mess for some reason (it looks as it's supposed to, before I made it scrollable).</p>
  <p>Here's an example constraint for my topBar (blue one), that's supposed to take up the entire horizontal space.</p>
  <pre><code>self.scrollView.addConstraints(
          NSLayoutConstraint.constraintsWithVisualFormat(
              &quot;H:|[topBar]|&quot;, options: nil, metrics: nil, views: viewsDictionary))
  </code></pre>
  <p>Any ideas why this doesn't work?</p>