title: Programmatic UIScrollview with Autolayout
body: <p>After reading the technical notes on apple's website and reading matt neuburg's book on programming iOS 11 with a UIScrollview held in place with Autolayout, I have not been able to fully understand the concept of how it all works. </p>
  
  <p>Basically what I want to have is a <strong>Scrollview</strong> that would have a child view <strong>ChildView</strong> where this child view then has a <strong>Textview</strong>.</p>
  
  <p>Below I have attached the mockup of what I am trying to achieve <strong>Programmatically</strong> no-nibs, no storyboards.</p>
  
  <p><a href="https://i.stack.imgur.com/fROFg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fROFg.png" alt="enter image description here"></a></p>
  
  <p>and as for the code, This is what I usually come up with:</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>let Scroller: UIScrollView = {
      let scroll = UIScrollView()
      scroll.translatesAutoresizingMaskIntoConstraints = false
      scroll.backgroundColor = UIColor.alizarinColor()
      return scroll
  }()
  
  // Content view
  
  let ContentView : UIView = {
  
      let content = UIView()
      content.translatesAutoresizingMaskIntoConstraints = false
      content.backgroundColor = UIColor.blue
      return content
  }()
  
  override func viewDidLoad() {
  
      super.viewDidLoad()
  
      self.view.addSubview(Scroller)
  
      // Auto layout
      Scroller.leftAnchor.constraint(equalTo: view.leftAnchor, constant:0).isActive = true
      Scroller.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
      Scroller.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
      Scroller.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
  
      Scroller.addSubview(ContentView)
      // Undefined Content view 
  }
  </code></pre>
  
  <p><strong>Please Note:</strong> for the <strong>ContentView</strong>, I normally define constraints to anchor the edges inside the scrollview but not in this case with Autolayout and the fact that I want it to scroll vertically upwards when the keyboard <code>becomesFirstResponder</code>. Another way I came up with this to try to work is to create a <code>UIView</code> that spans larger than the Scrollview to allow the child view to be a subview of this larger view that has the scroll view as its parent.</p>
  
  <p><strong>My Problem:</strong> How can I achieve this from here onwards? Any suggestions?
  I have been giving it a thought to something like this: (ContentView would be the larger view that will allow this to be scrollable, and the child view would be the 3rd child view in the hierarchy)</p>
  
  <p><a href="https://i.stack.imgur.com/5YcvA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5YcvA.png" alt="enter image description here"></a></p>