Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting UIScrollView zoom scale to fit the screen

My point is to copy the behaviour of many apps which uses photos where when UIScrollView with UIImageView inside is being presented it fits image to the size of screen even if this image is smaller.

I'm certain that this shouldn't be done with UIImageView frame but manipulated by UIScrollView but how...?


In my code I have a method called updateZoom which calculates the minimumZoomScale value and also assigns this value to the zoomScale property. I guess that this is the right place to do math and calculate zoomScale manually.

This is the method:

- (void)updateZoom {
    float minZoom = MIN(self.view.bounds.size.width / self.imageView.image.size.width, self.view.bounds.size.height / self.imageView.image.size.height);

    if (minZoom > 1) {
        minZoom = 1;
    }

    self.scrollView.minimumZoomScale = minZoom;
    self.scrollView.zoomScale = minZoom;
}

I hope that someone would give me a hit on how to set the zoomScale to fit the UIScrollView bounds.

like image 935
cojoj Avatar asked Sep 19 '14 06:09

cojoj


1 Answers

To set the zoom which fits the image size in scrollView set the the minimumZoom for scrollView using this : self.scrollView.minimumZoomScale = self.scrollView.frame.size.width/ self.imageView.frame.size.width;

Above code will fit your image in the scrollView Width.

like image 194
Aks Avatar answered Sep 23 '22 12:09

Aks