2
votes

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

2
@AnreaMaps Can you please share your user interface of ViewController class? - HardikS
@HardikS I added a screenshot - Andrea Maps
why you have given bottom constraints of select countries UIView to UITextField and UIScrollView both? Remove unnecessary bottom constraints of UIView to UIScrollVIew and let me know it is working or not. - HardikS
@HardikS I tried it but it still does not work, do you have any idea? - Andrea Maps

2 Answers

2
votes

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
0
votes

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