UIScrollView frame is different from iOS 8 and iOS 7

I implemented a view controller that simply displays a single image and allows the user to pinch to enlarge / pan the image. It works great on iOS 7, but on iOS 8, the scroll view frame is different in size, and the end result is that the image is very encrypted on the iPhone and reduced too much on the iPad when working on iOS 8. This is due to the frame width the scroll is 600pt, which is its size in the storyboard (Universal Storyboard using size classes). But I have autodetection restrictions that must provide scroll stretching to fill the available space - there should be 768pt on the iPad. This applies to iOS 7, but not to iOS 8.

This is the setting: In the Builder interface, I have a UIViewController that contains a UIView that contains a UIScrollView with autorun restrictions ending, leading, lower and upper places for the supervisor. 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); 

I found that if I put the same NSLog in viewDidAppear , the frame width would be correctly 768pt. I tried moving this code to viewWillAppear instead of viewDidLoad , but I get the same result - it's 600 in viewWillAppear . Thus, the scroll view is properly stretched to fill the display, but only after it appears on the screen. I need the correct frame size before the image appears on the screen so that I can calculate the correct minimum and maximum zoom values.

What can I do to fix this and make sure it works for iOS 7 and 8?

+7
ios autolayout ios8 uiscrollview uiimageview
source share
2 answers

I solved it. For some reason, viewDidLoad too early in the life cycle to get an adjusted scrolling frame on iOS 8, but again it works on iOS 7. viewWillAppear also too early, as well as viewWillLayoutSubviews .

To solve the problem, I just moved the code where I need to use the scroll frame size to viewDidLayoutSubviews , which will finally get the correct size when it appears.

+22
source share

I also struggled a lot with a similar problem. I solved it by changing the width of the contentView inside the scroll. In ViewDidLoad I call this method

 func setupWidthOfContentView(){ let screen = UIScreen.mainScreen().bounds // println("Width: \(screen.size.width) Height: \(screen.size.height)") var constW = NSLayoutConstraint(item: vwContentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: screen.size.width) vwContentView.addConstraint(constW) } 
+1
source share

All Articles