iPad modal view controller acting in portrait even though it's landscape

I’m having some difficulty presenting a ModalViewController in landscape mode. Basically it shows up in the proper interface orientation and rotates only when the device is held in landscape mode but the viewcontroller acts as if it is in portrait mode.

I’m presenting the modal like this

  • Rotate an object in its direction of motion
  • Child View Controller rotation methods not being called
  • iOS: Orientation check rotation on ViewDidAppear
  • How do I force a specific UIInterfaceOrientation on an individual view in a UINavigationController?
  • After rotation UIView coordinates are swapped but UIWindow's are not?
  • Rotate a UIImage or UIView to a particular angle, NOT an amount of angle
  • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
    
        LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
        [self.window.rootViewController presentModalViewController:lvc animated:NO];
    
        return YES;
    }
    

    In LoginViewController

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        NSLog(@"%@",NSStringFromCGRect(self.view.frame));
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }    
    

    The log results after a couple of rotations :

    2012-03-06 13:51:49.308 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
    2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
    2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
    2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}}
    2012-03-06 13:51:49.313 OrientationTest[3132:207] {{0, 20}, {1024, 748}}
    2012-03-06 13:51:49.314 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
    2012-03-06 13:51:49.315 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
    2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
    2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
    2012-03-06 13:51:51.647 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
    2012-03-06 13:51:51.648 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
    2012-03-06 13:51:53.481 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
    2012-03-06 13:51:53.482 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
    2012-03-06 13:51:53.897 OrientationTest[3132:207] {{0, 0}, {748, 1024}}
    2012-03-06 13:51:53.898 OrientationTest[3132:207] {{20, 0}, {748, 1024}}
    

    Basically the loginViewController acts as if it was in portrait mode. I have two text fields on this view and when i tap on one of them i want to move the view up so the keyboard is not displayed over the text field. In order to do this i have to modify the frame.origin.x instead of y because the axis are inverted (view is acting like portrait) and this is causing a lot of issues.

    Edit :

    If i change the modal presentation style on LoginViewController to UIModalPresentationPageSheet it works as intended so there is some issue with the fullscreen mode only

    Second Edit:

    I’ve stripped down the code to basics. I’m not even presenting a modal view controller anymore just initializing that view controller as the root view controller and still this is happening.

    App delegate :

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    In my view controller :

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        NSLog(@"%@",NSStringFromCGRect( self.view.frame));
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    

    3 Solutions Collect From Internet About “iPad modal view controller acting in portrait even though it's landscape”

    Ok , so i finally found a way for this to properly work. It seems if you just create a new view controller and present that it will always be inverted. The fix i found was the put the view controller in a navigation controller.

    LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:lvc];
    nvc.navigationBarHidden = YES;
    lvc.modalPresentationStyle = UIModalPresentationFullScreen;
    lvc.parentView = self.navController.view;
    [self.window.rootViewController presentModalViewController:nvc animated:NO];
    

    The frame being x/y inverted is a “feature” 😉 You are missing a convertRect:fromView. Have a look at converting rects and points between views.

    In your case, you need to convert the rect of the keyboard frame to your view controller’s primary view. Without seeing your keyboard frame stuff I don’t know exactly, but it might be something like this:

    [self.view convertRect:keyboardFrame fromView:keyboardView];
    

    change this

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        NSLog(@"%@",NSStringFromCGRect(self.view.frame));
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }    
    

    into this

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        NSLog(@"%@",NSStringFromCGRect(self.view.frame));
        return YES;
    }