title: Trouble getting UIImageView inside ScrollView programmatically
body: <p>After days of trying, and searching through countless SO/google/YouTube pages, I unclear how to accomplish this: I'm trying to place a single tall, narrow image inside a <code>UIScrollView</code> that only takes up a section of the screen, only scrolls vertically, and is preferably only coded programmatically, no Interface Builder at all.</p>
  
  <p>I've managed to create the <code>scrollView</code>, set the <code>backgroundColor</code> to blue so I can see it and managed to use constraint anchors to pin it exactly where I need it to be. I then added the top and bottom labels as every video tutorial was telling me to, but I've since deleted these as they didn't seem necessary once I added the image. </p>
  
  <p>The problems start as soon as I try to add the image. I've added an example image below as it's a tall, narrow image. </p>
  
  <p><a href="https://imgur.com/7qI1IaT" rel="nofollow noreferrer">https://imgur.com/7qI1IaT</a></p>
  
  <p>If you run the code with the image, you'll see:</p>
  
  <ol>
  <li><p>The image scrolls horizontally as well as vertically. I'd have thought <code>content.didOffset.x &lt; 0</code> would work, but apparently not. There's probably a simple method to fix this but I'm yet to find it. </p></li>
  <li><p>If the height of the image is less than the height of the <code>scrollView</code>, i want the image to stretch to fit the <code>scrollView</code>. I used both <code>.scaleAspectFit</code> and <code>.scaleAspectFill</code> and neither of these seemed to change anything.</p></li>
  <li><p>The width of the image (or at least, the image I'm using, not the example image) is larger than the section of <code>scrollView</code> I have, and it goes off the screen. Again, I'm sure there's an easy fix to this, but I don't know. </p></li>
  </ol>
  
  <p>Here is my code, but it's probably all wrong. </p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      lazy var scrollView: UIScrollView = {
          let view = UIScrollView()
          view.translatesAutoresizingMaskIntoConstraints = false
          view.frame.size.height = 3000
          view.backgroundColor = UIColor.blue
          return view
      }()
  
      let imageView: UIImageView = {
          let image = UIImageView(image:  imageLiteral)
          image.translatesAutoresizingMaskIntoConstraints = false
          return image
      }()
  
      func setupLayout() {
          scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
          scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 100).isActive = true
          scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
          scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  
          scrollView.addSubview(imageView)
  
          imageView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
          imageView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
          imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
          imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
  
          imageView.frame.size.height = scrollView.frame.size.height
          imageView.frame.size.width = scrollView.frame.size.width
  
          imageView.contentMode = .scaleAspectFit
          imageView.clipsToBounds = true
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.addSubview(scrollView)
  
          setupLayout()
      }
  }
  </code></pre>
  
  <p>I don't know if I'm doing the right thing by adding the image as a subview of <code>scrollView</code>. I couldn't get the image to scroll at all until I changed it from the subview of view to <code>scrollView</code>. The labels in the tutorials I've seen were added that way, and it made more sense to me to add it into the scrollView than the main screen view, but again, this could be wrong.</p>
  
  <p>I'm really not sure if it's the constraints, the <code>contentSize</code> or what, but it’s pretty clear I don't know what I’m doing, and I don't want to just wing it, so if anyone knows of any YouTube videos or websites that can help me out, I’d really appreciate it.</p>
  
  <p>Again, apologies. I feel like this is a really simple fix, but I just don't have it.</p>