Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView doesn't scroll in swift

I have an UIView and add an UIScrollView to it. Inside the scrollView is an UIImageView.
I want to zoom the image view by pressing a button. If the image view is zoomed you should be able to scroll but that's not working.

Currently I have that:

self.imageView = UIImageView()
self.imageView.image = image
self.imageView.frame = self.contentView.bounds

self.scrollView = UIScrollView()
self.scrollView.frame = self.contentView.bounds
self.scrollView.contentSize = self.contentView.bounds.size
self.scrollView.addSubview(self.imageView)

self.contentView.addSubview(self.scrollView)

and that:

@IBAction func zoomPicture() {
    if (scale <= 1.5) {
        scale = scale + 0.1
        scrollView.transform = CGAffineTransformMakeScale(scale, scale)
        scrollView.zoomScale = scale
        scrollView.maximumZoomScale = 1.5
        scrollView.minimumZoomScale = 1.0
        scrollView.contentSize = contentView.bounds.size
        scrollView.scrollEnabled = true
    }
}

my class also implements UIScrollViewDelegateand I set the delegate in the viewDidLoad() function. I have also added the viewForZoomingInScrollView function. The zoom works but I can't scroll.

like image 996
mafioso Avatar asked Aug 09 '16 06:08

mafioso


People also ask

Can't ScrollView scroll Swift?

You need to set the frame of your UIScrollView so that it is less than the contentSize . Otherwise, it won't scroll. If you are having the problem with swift 5.0+, you can go through freecodecamp.org/news/… article..

Why is ScrollView not scrolling?

To fix ScrollView Not scrolling with React Native, we wrap the content of the ScrollView with the ScrollView. to wrap ScrollView around the Text components that we render inside. As a result, we should see text inside and we can scroll up and down.

How do you make a scrollable view in Swift?

One way to do this is programmatically create an UIScrollView in your UIViewController . To control the scrollability you can set the ScrollView contentSize property.


1 Answers

For this issue you have to set self.scrollView.contentSize bigger than self.contentView.bounds so it get proper space to scroll.

replace this line

self.scrollView.contentSize = self.contentView.bounds.size

to this:

self.scrollView.contentSize = self.contentView.bounds.size*2 //or what ever size you want to set
like image 78
Ashish Thakkar Avatar answered Sep 21 '22 09:09

Ashish Thakkar