VH24
VH24

Reputation: 79

Zoom in a Scroll View

I'm a bit confused. I followed the recommendation given by Apple to zoom in a scroll view, but nothing happens.

Code:

import UIKit
import SpriteKit

class ScrollViewController: UIViewController, UIScrollViewDelegate {
    var scrollView: UIScrollView!
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView = UIImageView(image: UIImage(named: "image.png"))
        scrollView = UIScrollView(frame: view.bounds)
        scrollView.contentSize = imageView.bounds.size
        scrollView.addSubview(imageView)
        self.scrollView.delegate = self
        self.scrollView.minimumZoomScale = 0.5
        self.scrollView.maximumZoomScale = 3
        view.addSubview(scrollView)
    }

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

I'm probably missing something. However, the function to scroll vertically or horizontally works well. But when I press "option" and move the 2 grey dots, no zoom happens.

Upvotes: 1

Views: 3053

Answers (1)

Alex Shubin
Alex Shubin

Reputation: 3617

You typed wrong name for delegate method. Seems you copied that code from previous version of Swift. In Swift 3 it should be:

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

Be careful with such mistakes it can waste so much time. I recommend you to type every delegate method once again "viewforzoom..." and auto complete will show you the right one.

Upvotes: 1

Related Questions