解雇模态视图控制器时如何保持显示视图控制器的方向?

Mih*_*atu 32 iphone uiviewcontroller screen-rotation ios swift

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

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

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

  • 窗口 - 肖像
    • 根视图控制器(UINavigationController - Portrait)
      • 主视图控制器(UIViewController - Portrait)
        • 详细信息视图控制器(UIViewController - Portrait)
        • .
        • .
        • .
        • 模态视图控制器(UIVIewController - 全部)

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

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

override func supportedInterfaceOrientations() -> Int
{
    return Int(UIInterfaceOrientationMask.Portrait.toRaw())
}
Run Code Online (Sandbox Code Playgroud)

我的模态视图控制器再次覆盖此方法,它看起来像这样:

override func supportedInterfaceOrientations() -> Int
{
    return Int(UIInterfaceOrientationMask.All.toRaw())
}
Run Code Online (Sandbox Code Playgroud)

更新1

看起来这只发生在iOS8 Beta上.有人知道视图控制器的轮换是否有变化,或者这只是测试版中的错误?

ZaE*_*FaR 18

- (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;
}
Run Code Online (Sandbox Code Playgroud)

而对于斯威夫特来说

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

}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看此链接


jul*_*les 7

我在应用程序中遇到了同样的问题,经过几天的实验,我想出了一个不太好的解决方案,但现在可以使用.我application:supportedInterfaceOrientationsForWindow:在appdelegate中使用委托方法.

我创建了一个测试项目并将其放在github上(包括显示结果的GIF ...)

//注意:它不是很快但我希望无论如何都有帮助


mat*_*att 5

经过多次试验,我确信这是 iOS 8 的一个“特性”。

如果你仔细想想,这是完全有道理的,因为它已经出现了很长时间。

  • 例如,在 iOS 4 中,可以在更改选项卡栏控制器和导航控制器中的视图控制器以及呈现/关闭控制器时强制应用程序旋转。

  • 然后在 iOS 6 中,除非呈现/关闭视图控制器,否则不可能强制应用程序旋转(正如我在许多答案中所解释的,例如这个)。

  • 现在,在 iOS 8 中,我猜想根本不可能强制应用程序旋转(启动时除外)。它可以更喜欢某个方向,因此一旦它处于该方向,它就会留在那里,但它不能强制应用程序进入该方向。

    相反,您的视图控制器应该“适应”。有几个 WWDC 2014 视频专注于“适应”,现在我开始明白这是为什么这如此重要的原因之一。

    编辑:在种子 4 中,看起来这个功能(强制轮换演示和解雇)正在回归!

  • @matt 你有没有想出一种在 iOS 8 中强制旋转视图控制器的方法? (2认同)