简体   繁体   中英

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 eg 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!

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

I think what is happening is that the preferred orientation is set on the initial root controller - when you switch it out, it doesn't get set.

I would suggest you consider always making HomeViewController the root controller. And instead of switching the root controller, push the LoginViewController (with no animation if you want it to show up instantly):

 [self.navigationController pushViewController:controllerLoginBG animated:NO]

That way you'll be able to do things like logout and pop to the HomeViewController. It will also keep your view controller navigation consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM