UIScrollView not respecting minimumZoomScale after changing the subview
ios, objective-c, uiscrollview, uiscrollviewdelegate
Solution
The problem seems to be - not sure if this behavior is documented somewhere - resetting the content size while the current zoom scale is set to something other than 1.0.
The fix is simple: Reset the `zoomScale` property to 1.0 before setting the new content size:
// See http://stackoverflow.com/questions/10586577
// first reset before new content size
scroller.zoomScale = 1.0f;
// set new content size
scroller.contentSize = newImage.size;
// ... adjust minimum/maximum zoom scales if needed
Problem
I'm hitting a problem getting a UIScrollView to update correctly in response to a change in the minimum zoom scale. The scrollview has a UIImageView as a subview, and the image property of the UIImageView is set in response to the didFinishPickingMediaWithInfo method of UIPickerView: ``` - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *takenImage = [info objectForKey:UIImagePickerControllerOriginalImage]; [self.imageView setImage:takenImage]; [self.imageView setFrame:CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, takenImage.size.width, takenImage.size.height)]; [self.scrollView setContentSize:CGSizeMake(takenImage.size.width, takenImage.size.height)]; [self.scrollView setMinimumZoomScale:[self.scrollView frame].size.width / takenImage.size.width]; [self.scrollView setMaximumZoomScale:2.0]; [self.scrollView setZoomScale:[self.scrollView minimumZoomScale] animated:YES]; [self dismissModalViewControllerAnimated:YES]; } ``` This works correctly the first time an image is added using this method. However, if this method is triggered again - even to add the same image as the first time - the subsequent image is displayed at full size in the scrollView, and can't be zoomed out - only zoomed in. I've dumped the `contentSize`, `zoomScale`, `minimumZoomScale` and `maximumZoomScale` of the scrollView to the log, and they are the same each time. The `minimumZoomScale` is being calculated correctly each time. It's as if the scrollView is being redrawn with a `minimumZoomScale` of 1.0, ignoring the fact it's been explicitly set. Is there something obvious that I'm missing here?