Ios – make the UIImageView fit inside the UIScrollView without panning. [iOS]

iosobjective cuiimageviewuiscrollview

I have an image inside of an iOS UIScrollView that only takes up about 1/3rd of the total screen space. The image is too large, so the scrollView is allowing panning around the image. I want the image to be small enough so that there is no panning necessary to see the entire image. How do I resize the image to make it fit inside of the scroll view?

This is the code I am using to set up the image view and the scroll view.

UIImage *image = [UIImage imageNamed:@"banner.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.scrollView addSubview:self.imageView];

self.scrollView.contentSize = image.size;

Best Answer

You need to set the ImageView's size equal to the scrollView's size. And set the imageView's content mode to scale to fill Eg:

UIImage *image = [UIImage imageNamed:@"banner.png"];
self.imageView = [[UIImageView alloc] initWithImage:image]
self.imageView.frame = CGRectMake (0,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height);

self.imageView.contentMode = UIViewContentModeScaleToFill;
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = self.imageView.frame.size;
self.scrollView.bounces = NO;
Related Topic