問題描述
我實現(xiàn)了一個視圖控制器,它只顯示單個圖像并允許用戶通過捏合來放大/平移圖??像.它在 iOS 7 上運行良好,但在 iOS 8 上,滾動視圖的框架大小不同,最終結果是在 iOS 8 上運行時圖像在 iPhone 上放大得太遠而在 iPad 上縮小得太遠.這是因為滾動視圖框架的寬度是 600pt,這是它在情節(jié)提要中的大小(使用大小類的通用情節(jié)提要).但是我有自動布局約束,應該確保滾動視圖伸展以填充可用空間——在 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);
我發(fā)現(xiàn)如果我將相同的 NSLog
放在 viewDidAppear
中,則框架的寬度是正確的 768pt.我嘗試將該代碼移動到 viewWillAppear
而不是 viewDidLoad
,但我得到了相同的結果 - viewWillAppear
中也是 600.因此,滾動視圖正確拉伸以填充顯示,但直到它出現(xiàn)在屏幕上之后.在圖像出現(xiàn)在屏幕上之前,我需要正確的幀大小,以便計算正確的最小和最大縮放值.
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 上的大小不同的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!