UIScrollView的框架是在iOS 8不同的大小VS iOS的7 [英] UIScrollView frame is a different size on iOS 8 vs iOS 7

查看:89
本文介绍了UIScrollView的框架是在iOS 8不同的大小VS iOS的7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了只显示一个单一的形象,并允许用户捏来放大/平移图​​像周围视图控制器。它在iOS 7的伟大工程,但在iOS 8滚动视图的框架是不同的大小,而最终的结果是在iOS 8上运行时,这是因为图像是太远iPhone上的放大和iPad上的太远缩小滚动视图框的宽度为600pt,这是它在故事板尺寸(使用通用故事板尺寸类)。但我已经自动版式,都应该确保滚动视图拉伸以填充可用空间的限制 - 它应该是768pt在iPad上。这是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);

我发现,如果我把在 viewDidAppear 相同的的NSLog 帧的宽度是正确768pt。我试着动了code到 viewWillAppear中而不是 viewDidLoad中,但我得到了同样的结果 - 这是600 viewWillAppear中为好。因此,滚动视图正确拉伸以填补显示但直到它出现在屏幕上了。我需要正确的帧尺寸在屏幕上显示的图像之前,这样我可以计算适当的最小和最大缩放值。

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.

要解决这个问题,我刚刚搬到了code,我需要使用滚动视图的帧大小为 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不同的大小VS iOS的7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆