UIScrollView frame is a different size on iOS 8 vs iOS 7(UIScrollView 框架在 iOS 8 和 iOS 7 上的大小不同)
问题描述
我实现了一个视图控制器,它只显示单个图像并允许用户通过捏合来放大/平移图像.它在 iOS 7 上运行良好,但在 iOS 8 上,滚动视图的框架大小不同,最终结果是在 iOS 8 上运行时图像在 iPhone 上放大得太远而在 iPad 上缩小得太远.这是因为滚动视图框架的宽度是 600pt,这是它在情节提要中的大小(使用大小类的通用情节提要).但是我有自动布局约束,应该确保滚动视图伸展以填充可用空间——在 iPad 上应该是 768pt.在 iOS 7 上是这种情况,但在 iOS 8 上不是.
I implemented a view controller that simply displays a single image and allows the user to pinch to zoom in/pan around the image. It works great on iOS 7, but on iOS 8 the scroll view's frame is a different size, and the end result is the image is zoomed in too far on iPhone and zoomed out too far on iPad when running on iOS 8. This is because the scroll view frame's width is 600pt, which is its size in the storyboard (Universal Storyboard using size classes). But I have autolayout constraints that are supposed to ensure the scroll view stretches to fill the available space - it should be 768pt on iPad. That is the case on iOS 7 but not iOS 8.
这是设置:在 Interface Builder 中,我有一个 UIViewController
,其中包含一个 UIView
,其中包含一个带有自动布局约束的 UIScrollView
,超级视图的前导、底部和顶部空间.然后在 viewDidLoad
:
This is the setup: In Interface Builder, I have a UIViewController
that contains a UIView
which contains a UIScrollView
with autolayout contraints trailing, leading, bottom, and top space to the superview. Then in viewDidLoad
:
UIImage *image = [UIImage imageNamed:@"image"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
#warning Bug here - scroll view frame is 600pt on iOS 8 - should be 768 for iPad
NSLog(@"scroll view frame wid: %f", self.scrollView.frame.size.width);
我发现如果我将相同的 NSLog
放在 viewDidAppear
中,则框架的宽度是正确的 768pt.我尝试将该代码移动到 viewWillAppear
而不是 viewDidLoad
,但我得到了相同的结果 - viewWillAppear
中也是 600.因此,滚动视图正确拉伸以填充显示,但直到它出现在屏幕上之后.在图像出现在屏幕上之前,我需要正确的帧大小,以便计算正确的最小和最大缩放值.
I discovered if I put that same NSLog
in viewDidAppear
the frame's width is correctly 768pt. I tried moving that code into viewWillAppear
instead of viewDidLoad
, but I get the same outcome - it's 600 in viewWillAppear
as well. So the scroll view is properly stretching to fill the display but not until after it appears on screen. I need the correct frame size before the image appears on screen so that I may calculate the proper min and max zoom values.
我能做些什么来解决这个问题并确保它适用于 iOS 7 和 8?
What can I do to fix that and ensure it works for iOS 7 and 8?
推荐答案
我已经解决了.出于某种原因,viewDidLoad
在生命周期中太早了,无法在 iOS 8 上获取滚动视图的调整帧,但在 iOS 7 上它再次起作用. viewWillAppear
也太早了,以及 viewWillLayoutSubviews
.
I've solved it. For some reason, viewDidLoad
is too early in the life cycle to obtain the scroll view's adjusted frame on iOS 8, but again it works on iOS 7. viewWillAppear
is also too early, as well as viewWillLayoutSubviews
.
为了解决这个问题,我只是将需要使用滚动视图的帧大小的代码移到 viewDidLayoutSubviews
中,它最终会获得正确的大小.
To solve the problem, I just moved the code where I need to use the scroll view's frame size to viewDidLayoutSubviews
which does finally obtain the correct size it will be when it appears.
这篇关于UIScrollView 框架在 iOS 8 和 iOS 7 上的大小不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 框架在 iOS 8 和 iOS 7 上的大小不同
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01