0

I am working on a sample to display images inside scroll. The image is loading fine but then it allows me to drag the image up and down inside the scroll. I would still want to be able to move right and left (horizontally), but the image should be fixed from moving vertically.

The scroll view and the image constraints are set to the top, bottom, left and right as those of the main view.

What am I doing wrong?

This is my code and storyboard settings

import UIKit

class Image2ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.scrollView.contentSize = (imageView.image?.size)!
    }
}

and these are my storyboard settings for ScrollView and ImageView. The top 2 are for the scroll and other 2 are for the image.

scroll image1

enter image description here

enter image description here

enter image description here

user1019042
  • 2,428
  • 9
  • 43
  • 85
  • 1
    Take a look at this question: https://stackoverflow.com/questions/5271521/how-to-lock-the-horizontal-scrolling-of-a-scrollview-in-ios – kmell96 Apr 06 '18 at 23:32
  • print(scrollView.frame) then print(scrollView.contentSize). If your content width is greater than your frame width the scrollView will scroll left to right. This is how I would debug your situation. – Aaron Halvorsen Apr 07 '18 at 00:17
  • @kmell96 tracing other posts mentioned in the post you gave me pointed me to the solution, so thank you! – user1019042 Apr 07 '18 at 02:40

1 Answers1

0

Adding this line in the viewDidload solved the issue: self.automaticallyAdjustsScrollViewInsets = false

user1019042
  • 2,428
  • 9
  • 43
  • 85