UIScrollView does not get a new size when changing orientation

I have a very simple UIScrollView example that just doesn't do what it was supposed to do. Not sure if this is an API error or an error in my code.

Basically, I have a UIViewController with a UIScrollView as I view it. When I add it to UIWindow and change the orientation of the iPad, I exit the UIScrollView format, which is incorrect (?).

Here is my implementation of the UIViewController :

 @implementation CustomViewController - (void)loadView { scrollView = [[[UIScrollView alloc] init] autorelease]; scrollView.delegate = self; scrollView.backgroundColor = [UIColor redColor]; self.view = scrollView; } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { CGSize rect = scrollView.frame.size; NSLog(@"will rotate w%fh%f", rect.width, rect.height); } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { CGSize rect = scrollView.frame.size; NSLog(@"will animate rotation w%fh%f", rect.width, rect.height); } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { CGSize rect = scrollView.frame.size; NSLog(@"did rotate w%fh%f", rect.width, rect.height); } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } @end 

When I run the above code, I see the following entries in my console:

 2010-07-11 11:03:05.214 Untitled2[6682:207] will rotate w768.000000 h1004.000000 2010-07-11 11:03:05.214 Untitled2[6682:207] will animate rotation w748.000000 h1024.000000 2010-07-11 11:03:05.619 Untitled2[6682:207] did rotate w748.000000 h1024.000000 2010-07-11 11:03:07.951 Untitled2[6682:207] will rotate w748.000000 h1024.000000 2010-07-11 11:03:07.958 Untitled2[6682:207] will animate rotation w768.000000 h1004.000000 2010-07-11 11:03:08.367 Untitled2[6682:207] did rotate w768.000000 h1004.000000 

As you can see, orientation changes resize the UIScrollView , but only to allow a new status bar. I would expect the width and height to change dramatically since the UIScrollView is both wider than tall and vice versa.

Is there any way to get UIScrollView to report the real size about it?

+7
ios ipad uiscrollview orientation
source share
5 answers

I have an answer if you are still looking. Check the borders , not the frame.

Reason: frame is a derived boundary property and transformation used for rotation.

+3
source share

Views will not automatically resize when their supervisors are resized unless you specifically set them. For this type of scrolling, you will need to specify that it changes its width and height when its supervisor does.

 scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
+2
source share

I noticed that defining a new contentSize in didRotateFromInterfaceOrientation also not possible if you start with a NIB file for iPhone instead of a dedicated NIB file for iPad: the frames in the NIB file for iPhone are much smaller, and therefore the estimated content height is much larger than it should be when you are actually on an iPad. The Apple DID says I MUST create a separate NIB file for the iPad ...

So, I also get the old measurements in didRotateFromInterfaceOrientation , but for me this is no longer a problem since iOS processes everything OK as soon as I start with the desired NIB file. However, it seems less intuitive to me that in the didRotateFromInterfaceOrientation method, the didRotateFromInterfaceOrientation sizes are incorrect for the current view.

PS. I found a solution at the end, just use notifications instead of didRotateFromInterfaceOrientation .

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

Then, in the orientationChanged method, adjust the views to new frames. I had one last problem: if the initial orientation was different from the orientation in nib, the view would still be wrong, I decided that creating a new NIB file, especially for landscape view. In the init method, the ViewController method selects which NIB it loads based on the current orientation.

+1
source share

I usually fix this by setting the single value of contentSize to 0 as CGSizeMake(contentSize.width, 0) for horizontal viewing.

To make UIScrollView scroll only, for example. The horizontal value does not scroll for contentSize to 0> CGSizeMake(contentSize.width, 0) .

Hope this helps.

0
source share

I don’t know that this will help you because I have never tested it, but I think it can help you.

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { self.view = scrollView; CGSize rect = scrollView.frame.size; NSLog(@"will rotate w%fh%f", rect.width, rect.height); } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { self.view = scrollView; CGSize rect = scrollView.frame.size; NSLog(@"will animate rotation w%fh%f", rect.width, rect.height); } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { self.view = scrollView; CGSize rect = scrollView.frame.size; NSLog(@"did rotate w%fh%f", rect.width, rect.height); } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } 
0
source share

All Articles