iOS UIScrollView Zoom In/Out UIImageView

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Create UIScrollView instance

let scrollview = UIScrollView.init(frame: self.view.bounds)

And then set these properties:

scrollView.minimumZoomScale = 0.1
scrollView.maximumZoomScale = 4.0
scrollView.zoomScale = 1.0
scrollview.delegate = self as? UIScrollViewDelegate 

To zoom in and out image we must specify the amount the user can zoom in and out. We do this by setting values of the scroll view’s minimumZoomScale and maximumZoomScale properties. Both of these are set to 1.0 by default.

And zoomScale to 1.0 which specify the zoom factor for the minimum and maximum zooming.

To support zooming, we must set a delegate for your scroll view. The delegate object must conform to the UIScrollViewDelegate protocol. That delegate class must implement the viewForZoomingInScrollView() method and return the view to zoom.

Modify your ViewController as shown

class ViewController: UIViewController, UIScrollViewDelegate

Then add the following delegate function to the class.

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return imageView
}

Now create UIImageView instance

Make this variable as class variable

var imageView:UIImageView = UIImageView.init(image: UIImage.init(named: "someImage.jpg"))

And then add it to scrollview

scrollView?.addSubview(imageView)

Reference



Got any iOS Question?