Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6 Rotation Bug: Window and Root View Controller frame does not change despite rotation [duplicate]

Tags:

ios

rotation

ios6

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:

  1. Create a new empty project
  2. Add a bone stock UIViewController as the root view
  3. Set a timer to print out the root view controller's frame width and height
  4. Run your app, and rotate it all around. Here's my code I'm testing with:
- (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);
}
like image 682
user441669 Avatar asked Nov 12 '22 18:11

user441669


1 Answers

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;
like image 53
iMathieuB Avatar answered Dec 09 '22 12:12

iMathieuB