Possible Duplicate:
“Incorrect” frame / window size after re-orientation in iPhone
I'm trying to solve a very frustrating issue with an app of mine - in studying it I may have found a bug in iOS:
When you rotate the iPad, the view rotates, but it's frame does not change. I am running a timer that prints out the width and height of my view every second, and although I can see the view rotate visually, the frame remains "768x1024" no matter what.
From Apple's docs:
When the user interface rotates, the window is resized to match the new orientation. The window adjusts the frame of its root view controller to match the new size, and this size in turn is propagated down the view hierarchy to other views.
This is exactly what isn't happening, and it's causing me all sorts of trouble. Does anyone know how to fix rotation so that the frame changes?
Steps to reproduce:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[UIViewController alloc] init]; NSLog(@"%fx%f", self.window.rootViewController.view.frame.size.width, self.window.rootViewController.view.frame.size.height); // Add a label to confirm orientation UILabel *orientationLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,30)]; orientationLabel.text = @"Top Left"; [self.window.rootViewController.view addSubview: orientationLabel]; [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(testScale) userInfo:nil repeats:YES]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
- (void) testScale { NSLog(@"%fx%f", self.window.rootViewController.view.frame.size.width, self.window.rootViewController.view.frame.size.height); }
Did you try to put your UIViewController inside a UINavigationController instead? I had a similar issue and it fixed it. The UINavigationController class seems to handle better rotations than UIViewController.
Try something like this:
UIViewController *myViewController = [[UIViewController alloc] init];
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window.rootViewController = myNavigationController;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With