Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView not getting new size on orientation change

I have a very simple UIScrollView example that simply doesn't do what it's supposed to. Not sure if it's a bug with the API or a bug in my code.

Basically, I've got a UIViewController with a UIScrollView as it's view. When I add it to the UIWindow and change the orientation of the iPad I log out the UIScrollViews size, which is incorrectly(?) reported.

Here's my UIViewController implementation:

@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%f h%f", rect.width, rect.height);
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  CGSize rect = scrollView.frame.size;
  NSLog(@"will animate rotation w%f h%f", rect.width, rect.height);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  CGSize rect = scrollView.frame.size;
  NSLog(@"did rotate w%f h%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, the orientation changes does resize the UIScrollView, but only to allow the new statusbar. I would expect that the width and height drastically change, as the UIScrollView is how wider than it is high and vice versa.

Is there a way to get the UIScrollView to report it's real size?

like image 324
d2kagw Avatar asked Jul 11 '10 01:07

d2kagw


1 Answers

I have the answer if you are still looking. Test the bounds, not the frame.

Reason: frame is a derived property of the bounds and the transform being applied for rotation.

like image 74
Preston Software Avatar answered Oct 03 '22 17:10

Preston Software