简体   繁体   中英

Switch to a differnet UIViewController when the device orientation changes

I am currently building an app for iPad where the view structure is completely different depending on the device orientation.

When the app is in portrait I am using the ECSlidingViewController library to support left and right sliding menu's (like facebook or path).

When the app is in landscape I need to display a splitviewcontroller so that the left hand menu is always visible.

Does anybody know of the best solution to this problem?

I have tried changing the RootViewController of the UIWindow when an orientation change is detected but this gave some very strange results....

There are several ways to do this. Some better than other, but mostly it will depends on the needs of your specific situation.

  1. Replace the rootViewController as you mentioned during rotation.
  2. Always use a UISplitViewController and use a segue to with replace to replace the detailViewController when rotating between portrait and landscape.
  3. Use/make a custom container view controller which adds/removes the appropriate view controller on rotation using addChildViewController: and removeFromParentViewController:

Extremely crude example of choice 3.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.childViewControllers[0].view removeFromSuperView];
    [self.childViewControllers[0] removeFromParentViewController];

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        [self addChildViewController:portraitVC];
        [self.view addSubview:portraitVC.view];
    } else {
        [self addChildViewController:landscapeVC];
        [self.view addSubview:landscapeVC.view];
    }
}

From what you've said, I'd guess option 3 would be the best fit. All this said though, I'm not entirely convinced you need to be switching view controllers, but I'm not familiar with ECSlidingViewController and what it offers so I'm can't be certain.

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