5

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);
}
Community
  • 1
  • 1
user441669
  • 1,054
  • 1
  • 10
  • 23
  • duplicate of http://stackoverflow.com/questions/2686882/incorrect-frame-window-size-after-re-orientation-in-iphone and http://stackoverflow.com/questions/11150495/ios-uiview-get-frame-after-rotation – Bot Nov 09 '12 at 15:46

3 Answers3

1

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;
iMathieuB
  • 1,168
  • 10
  • 11
  • 1
    I wanna thank you. I have been looking for 2 days online to find if anywhere someone could explain why my 5.1 app completely broke down in 6.1. I was getting a black bar between navigationBar and UIView, and navigationBar was NOT rotating, whereas the rest of the view did rotate. This simple magic line: self.window.navigationController = self.navigationController; in AppDelegate fixed it for me! Again, Thank you so much ! – Ælex Mar 30 '13 at 19:50
0

Not a bug. What you are wanting is to use bounds instead of frame

Think of it this way. When you rotate your device, the device is not changing its screen size. It is staying the same, however the view is changing. So this is why you would want to use bounds.

Bot
  • 11,868
  • 11
  • 75
  • 131
  • It seems to explicitly counteract the Apple iOS 6 docs, which say "adjusts the frame of its root view controller" – user441669 Nov 09 '12 at 01:27
  • @user441669 there are numerous duplicate questions that state this very thing. – Bot Nov 09 '12 at 15:45
0

You can use the state of the Status Bar

 [[UIApplication sharedApplication] statusBarOrientation]

with an if statement, you can know if the screen is Landscape or Portrait

Mutawe
  • 6,464
  • 3
  • 47
  • 90