UIScrollView not respecting minimumZoomScale after changing the subview

那年仲夏 提交于 2020-01-13 11:55:11

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/10586577/uiscrollview-not-respecting-minimumzoomscale-after-changing-the-subview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!