解除模态视图控制器时如何保持呈现视图控制器的方向? [英] How to maintain presenting view controller's orientation when dismissing modal view controller?

查看:24
本文介绍了解除模态视图控制器时如何保持呈现视图控制器的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发这个应用程序,我需要我所有的视图控制器,但一个是纵向的.一个特殊的单一视图控制器,我需要它能够旋转到手机所处的任何方向.

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.

为此,我以模态方式呈现它(未嵌入到 NavigationController 中)

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

所以(例如)我的结构是这样的:

So (for example) my structure is like this:

  • 窗口 - 纵向
    • 根视图控制器(UINavigationController - Portrait)
      • 主视图控制器(UIViewController - Portrait)
        • 细节视图控制器(UIViewController - Portrait)
        • .
        • .
        • .
        • 模态视图控制器(UIVIewController - 全部)

        现在,当我在横向位置关闭我的模态视图控制器时,即使它不支持该方向,我的父视图控制器也会旋转.

        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.

        应用程序中的所有 UIViewControllersUINavigaionControllers 都继承自实现了这些方法的相同通用类:

        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())
        }
        

        更新 1

        这似乎只发生在 iOS8 Beta 上.有人知道视图控制器的旋转是否发生了变化,或者这只是测试版中的一个错误?

        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?

        推荐答案

        - (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;
        }
        

        对于 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

        这篇关于解除模态视图控制器时如何保持呈现视图控制器的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆