Solution to ScrollView failure after IOS6

Source: Internet
Author: User

Today, I made a small ScrollView example (my environment Xcode5.0.2 IOS7), and found that the scrolling fails. Even if the contentSize of scrollView is set, I did some research, finally, the solution was found: Before ios6, because Xcode does not have the Autolayout mechanism, you can directly use scrollView and set its contentSize to scroll normally after ios6, because Xcode introduces the Autolayout mechanism, the contentSize we set is changed to a value suitable for the screen size, that is, adaptive, so it cannot be rolled. The solution is as follows: remove Autolayout of scrollView directly, but this method is not perfect. After all, Autolayout is officially recommended and easily removed will cause other problems, so use it with caution. In viewController, reload the-(void) viewDidAppear :( BOOL) animated method and set the contentSize. The Code is as follows: Copy code 1 # import "ImaginariumViewController. h "2 3 @ interface ImaginariumViewController () 4 @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; 5 @ property (weak, nonatomic) IBOutlet UIImageView * imageView; 6 @ end 7 8 @ implementation ImaginariumViewController 9 10-(void) viewDidAppear :( BOOL) animated11 {12 [su Per viewDidAppear: animated]; 13 self. scrollView. contentSize = self. imageView. image. size; 14 self. imageView. frame = CGRectMake (0, 0, self. imageView. image. size. width, self. imageView. image. size. height); 15} 16 17 @ end copy Code. In addition, I attached a small experiment: Copy code 1 # import "ImaginariumViewController. h "2 3 @ interface ImaginariumViewController () 4 @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; 5 @ property (wea K, nonatomic) IBOutlet UIImageView * imageView; 6 @ end 7 8 @ implementation ImaginariumViewController 9 10-(void) viewDidLoad11 {12 [super viewDidLoad]; 13 NSLog (@ "viewDidLoad % g", self. scrollView. contentSize. width, self. scrollView. contentSize. height); 14 self. scrollView. contentSize = self. imageView. image. size; 15} 16 17-(void) viewWillAppear :( BOOL) animated18 {19 [super viewWillAppear: animated]; 20 NSLog (@ "viewWillAppear % g", self. scrollView. contentSize. width, self. scrollView. contentSize. height); 21 self. scrollView. contentSize = self. imageView. image. size; 22} 23 24-(void) viewDidAppear :( BOOL) animated25 {26 [super viewDidAppear: animated]; 27 NSLog (@ "viewDidAppear % g", self. scrollView. contentSize. width, self. scrollView. contentSize. height); 28 self. scrollView. contentSize = self. imageView. image. Size; 29 self. imageView. frame = CGRectMake (0, 0, self. imageView. image. size. width, self. imageView. image. size. height); 30} 31 32-(void) didReceiveMemoryWarning33 {34 [super didReceiveMemoryWarning]; 35 // Dispose of any resources that can be recreated.36} 37 38 @ end copy the code experiment results, console output: Experiment Analysis: we can see that contentSize is set after the viewDidLoad method and the viewWillAppear method, but it is found that the settings in viewDidLoad are valid, at the beginning of viewWillAppear, the log value is exactly 70. 0 655, and after contentSize is set in viewWillAppear, viewDidAppear restores contentSize to 0 0, so I guess viewDidAppear is the place where Autolayout is actually executed, therefore, we need to set the contentSize after the viewDidAppear method calls [super viewDidAppear: animated. This experiment is an explanation of the second solution!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.