ios - 解除模态视图 Controller 时如何保持呈现 View Controller 的方向?

标签 ios iphone uiviewcontroller swift screen-rotation

我正在开发这个应用程序,我需要所有的 View Controller ,但需要一个纵向 View Controller 。 一个特别的单一 View Controller ,我需要它能够旋转到手机的任何方向。

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

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

  • 窗口 - 纵向
    • Root View Controller (UINavigationController - 纵向)
      • 主视图 Controller (UIViewController - 纵向)
        • 细节 View Controller (UIViewController - 纵向)
        • >。
        • .
        • .
        • 模态视图 Controller (UIVIewController - 全部)

现在,每当我在横向位置关闭模态视图 Controller 时,我的父 View Controller 也会旋转,即使它不支持该方向。

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

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

我的模态视图 Controller 再次覆盖了这个方法,它看起来像这样:

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

更新 1

看起来这只发生在 iOS8 Beta 上。 有人知道 View Controller 的旋转是否发生了变化,还是这只是测试版中的一个错误?

最佳答案

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

}

有关更多详细信息,请查看此 link

关于ios - 解除模态视图 Controller 时如何保持呈现 View Controller 的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229677/

相关文章:

ios - 从 SKScene 获取 UIViewController

ios - 将标记从 UITableViewController 添加到 UIViewController

xcode - Interface Builder放置了许多项目

ios - 使用NSOutputStream随机EXC_BREAKPOINT

ios - 拍摄和显示照片的应用程序内存错误

iphone - 如何在 Objective-C 中创建字符串或 float 组

ios - 在 iOS 中从 .xib 文件调用 UIViewController 时尝试传递 UIButton 的标记值

ios - 如何在 NavigationController 和 UITabbar 上方显示 UIView?

ios - 由于条件绑定(bind)错误的初始化程序,无法将文本 append 到字符串数组

iPhone开发: How to create colored or translucent background for UIActionSheet?