Search code examples
iosipaduiviewcontrolleruiwindow

UIWindow rootviewcontroller - wrong orientation


I am developing an iPad application where the user just can (in some cases) open the app, if he types in a password. Therefore I need something like a LoginViewController. But first, let us handle the common case, where the app just needs to display the HomeViewController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
    //...
    self.controllerHomeView = [[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil];
    self.controllerHomeView.showInitialGuide = isFirstLaunch;
    self.window.rootViewController = controllerHomeView;
    [self.window makeKeyAndVisible];
    //..
}

But, like I said before, some users may have defined a password and if so, we need to display a login screen. That's what I did to enable such a feature:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    //---
    if(isAppPinSecured && !loginIsAlreadyDisplaying) {
        LoginBackgroundViewController *controllerLoginBG = [[LoginBackgroundViewController alloc] initWithNibName:@"LoginBackgroundView" bundle:nil];
        self.appPinInputBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default-Landscape@2x~ipad.png"]];
        self.appPinInputBG.frame = CGRectMake(0, 0, 1024, 748);
        self.appPinInputBG.userInteractionEnabled = YES;
        [controllerLoginBG.view addSubview:self.appPinInputBG];
        //present new root = background
        self.window.rootViewController = controllerLoginBG;
        //...
    }
}

What I am doing is, changing the root view controller to LoginViewController and if the user puts in the correct password, changing back from the LoginViewController to the HomeViewController.

Everything works fine so far, except the orientation. If the current interface orientation is unknown, because the iPad e.g. is laying on a table, the LoginViewController orientation is LandscapeRight instead of the one in the HomeViewController (LandscapeLeft). It works properly if holding the iPad in your hands, but otherwise precisely not.

Any suggestions on how to fix that issue? I did set my app orientation in the plist file (Landscape Left). I do implement shouldAutorotate with UIInterfaceOrientationIsLandscape(...) in both - Home- and LoginViewController.

Thanks in advance!


Solution

  • Argh, the problem was, that I tried to push a view controller modally before viewDidAppear was called -> you should never do this...

    After changing my code due to this error it worked like a charm