Andrea Maps
Andrea Maps

Reputation: 97

Make UIView scrollable and zoomable

I'm trying to make a UIView scrollable and zoomable, I embedded in a scroll view and set its delegate but it does not work.

I'd like that "selectCountries" UIView can be scrolled and zoomed in and out like an image or similar.

How can achieve this result?

Here is what I've done so far:

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet var selectCountries: VTCSelectCountryView!
    @IBOutlet var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        scrollView.delegate = self
        scrollView.backgroundColor = .clear

        self.selectCountries?.selectedCountries = ["ITA","USA","GBR","FRA"]
    }

    override func viewWillLayoutSubviews(){
           super.viewWillLayoutSubviews()
           scrollView.isScrollEnabled = true
           // scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height+60)

       }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return self.selectCountries
    }
}

EDIT 1: I tried by adding the values written in the comments below but it still does not zoom and scroll the view.

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet var selectCountries: VTCSelectCountryView!
    @IBOutlet var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        scrollView.delegate = self
        scrollView.backgroundColor = .clear

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 10.0

        scrollView.bounces = true
        scrollView.bouncesZoom = true
        scrollView.contentSize = CGSize(width: selectCountries.frame.size.width, height: selectCountries.frame.size.height)
        scrollView.showsVerticalScrollIndicator = true
        scrollView.showsHorizontalScrollIndicator = true

        self.selectCountries?.selectedCountries = ["ITA","USA","GBR","FRA"]
    }

    override func viewWillLayoutSubviews(){
           super.viewWillLayoutSubviews()
           scrollView.isScrollEnabled = true
           // scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height+60)
       }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return self.selectCountries
    }
}

EDIT 2: Here is a screenshot of the storyboard

Storyboard-Scrollview_screenshot

Upvotes: 2

Views: 324

Answers (2)

Ruchi Makadia
Ruchi Makadia

Reputation: 1083

set ZoomScale and add subview's contentsize in your viewDidLoad()

    scrollview.maximumZoomScale = 4
    scrollview.minimumZoomScale = 1.0
    scrollview.bounces = true
    scrollview.bouncesZoom = true
    scrollview.contentSize = CGSize(width: imagerepoart.frame.size.width, height: imagerepoart.frame.size.height)
    scrollview.showsVerticalScrollIndicator = true
    scrollview.showsHorizontalScrollIndicator = true

Upvotes: 0

HardikS
HardikS

Reputation: 714

You need to also set the minimum and maximum zoom scale of UIScrollView

Set zoom scale in your viewDidLoad() method

scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 10.0

Upvotes: 2

Related Questions