IOS中方向更改后保存UIImageView的UIScrollView布局不正确
我已经设置了一个IOS中方向更改后保存UIImageView的UIScrollView布局不正确,ios,ios7,uiscrollview,uiimageview,uiinterfaceorientation,Ios,Ios7,Uiscrollview,Uiimageview,Uiinterfaceorientation,我已经设置了一个uicrollview,其中包含一个带有UIImage的UIImageView。我加载到图像视图中的UIImage的大小约为2300x1200,并且没有缩放 UIScrollView保存一个UIImageView,该视图设置为允许点击/双击放大和缩小图像。我有 示例的源代码可以是 问题 我遇到的问题是方向的改变。视图不再按预期和偏移进行布局 下载图像后(在我的示例中),我完成以下操作: [self.mainImageView setImage:image]; self.mainI
uicrollview
,其中包含一个带有UIImage
的UIImageView
。我加载到图像视图中的UIImage的大小约为2300x1200,并且没有缩放
UIScrollView保存一个UIImageView,该视图设置为允许点击/双击放大和缩小图像。我有
示例的源代码可以是
问题我遇到的问题是方向的改变。视图不再按预期和偏移进行布局 下载图像后(在我的示例中),我完成以下操作:
[self.mainImageView setImage:image];
self.mainImageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
// Tell the scroll view the size of the contents
self.mainScrollView.contentSize = image.size;
[self setupScales];
self.mainScrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.mainImageView.translatesAutoresizingMaskIntoConstraints = NO;
然后,我将比例尺设置如下:
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.mainScrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.mainScrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.mainScrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.mainScrollView.minimumZoomScale = minScale;
self.mainScrollView.maximumZoomScale = 1.0f;
self.mainScrollView.zoomScale = minScale;
[self centerScrollViewContents];
CGSize boundsSize = self.mainScrollView.bounds.size;
CGRect contentsFrame = self.mainImageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.mainImageView.frame = contentsFrame;
然后,我将按如下方式查看centerScrollViewContents:
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.mainScrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.mainScrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.mainScrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.mainScrollView.minimumZoomScale = minScale;
self.mainScrollView.maximumZoomScale = 1.0f;
self.mainScrollView.zoomScale = minScale;
[self centerScrollViewContents];
CGSize boundsSize = self.mainScrollView.bounds.size;
CGRect contentsFrame = self.mainImageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.mainImageView.frame = contentsFrame;
scrollView的左/右/上/下约束固定为0,而imageView没有手动创建的约束。我已尝试运行willRotateToInterfaceOrientation
中的self.main ScrollView needsUpdateConstraints
,但这没有任何区别
当选择第一个选项“图像缩放”,然后更改设备方向时,上述示例应用程序中也会出现同样的问题。我不确定什么是不正确的,似乎帧设置不正确。我尝试在不同的点运行setupScales,但这似乎并没有改变问题。当帧更改时,滚动视图的contentSize似乎被重置。旋转完成后,我必须将其设置回图像大小
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// When the orientation is changed the contentSize is reset when the frame changes. Setting this back to the relevant image size
self.mainScrollView.contentSize = self.mainImageView.image.size;
// Reset the scales depending on the change of values
[self setupScales];
}
我已经为将来需要的任何人建立了回购协议-