Ios – How to maintain presenting view controller’s orientation when dismissing modal view controller

iosiphonescreen-rotationswiftuiviewcontroller

I have this app I am working on and I need ALL my view controllers but one to be in portrait.
The single one view controller that is special I need it to be able to rotate to whatever orientation the phone is in.

To do that I present it modally (not embedded in a NavigationController)

So (for example) my structure is like this:

  • window – Portrait
    • root view controller (UINavigationController – Portrait)
      • home view controller (UIViewController – Portrait)
        • details view controller (UIViewController – Portrait)
        • .
        • .
        • .
        • modal view controller (UIVIewController – All)

Now when ever I dismiss my modal view controller in a landscape position my parent view controller is ALSO rotated even though it doesn't support that orientation.

All UIViewControllers and UINavigaionControllers in the app inherit from the same general classes which have these methods implemented:

override func supportedInterfaceOrientations() -> Int
{
    return Int(UIInterfaceOrientationMask.Portrait.toRaw())
}

My modal view controller overrides this method once again and it looks like this:

override func supportedInterfaceOrientations() -> Int
{
    return Int(UIInterfaceOrientationMask.All.toRaw())
}

Update 1

It looks like this is happening only on iOS8 Beta.
Does someone know if there is something that changed regarding view controller's rotation or is this just a bug in the beta?

Best Answer

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [SecondViewController class]])
{
    SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;

    if (secondController.isPresented)
        return UIInterfaceOrientationMaskAll;
    else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}

And for Swift

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {

    if self.window?.rootViewController?.presentedViewController? is SecondViewController {

        let secondController = self.window!.rootViewController.presentedViewController as SecondViewController

        if secondController.isPresented {
            return Int(UIInterfaceOrientationMask.All.toRaw());
        } else {
            return Int(UIInterfaceOrientationMask.Portrait.toRaw());
        }
    } else {
        return Int(UIInterfaceOrientationMask.Portrait.toRaw());
    }

}

For more details check this link

Related Topic