Search code examples
iosswiftuiscrollviewuiimageviewuipageviewcontroller

Scroll view not being able to zoom in or out in swift


I'm trying to get my image to be able to zoom in and zoom out inside a page view controller.

Layers:

--UIPageViewController

----CustomPageContentController

------ScrollView

--------UIImageView

----------UIImage

The image is showing fine, but I'm not able to zoom in or zoom out via a pinch.

How is this action enabled via code in swift?

This is what I currently have:

class PublicationPageView: UIViewController, UIScrollViewDelegate{

    var pageIndex:Int = 0
    var publicationImageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        println("pageIndex: \(pageIndex)")
        publicationImageView.contentMode = UIViewContentMode.ScaleAspectFit


        // Need to reposition the image
        publicationImageView.frame = CGRectMake(0, 0, view.frame.width, view.frame.height-65-65)
        println(publicationImageView.bounds.size)


        var scrollView = UIScrollView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height-65-65))

        scrollView.addSubview(publicationImageView)
        scrollView.contentMode = UIViewContentMode.ScaleAspectFit
        scrollView.maximumZoomScale = 4.0
        scrollView.minimumZoomScale = 1.0

        self.view.addSubview(scrollView)



    }

}

Is this a best practice way? Or would there be a better way to be doing this? I'm open to suggestions.

Thanks in advance


Solution

  • You need to implement a UIScrollViewDelegate method

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

    Here you return the UIView to be considered for zooming.