除1之外,将所有屏幕方向锁定为纵向

时间:2017-03-20 12:50:23

标签: ios objective-c iphone swift

我正在开发一个只能在纵向模式下访问的iOS应用。

我使用的1个框架(我的地图中80个屏幕中的1个)除了需要景观支持。因此我必须在我的plist中允许它。

确保所有其他视图以纵向显示并且只能以纵向显示的最简单方法是什么?

我的项目的一个好处是所有其他ViewControllers都继承自ProjectViewController。

Swift中的答案是首选。

2 个答案:

答案 0 :(得分:4)

class ProjectViewController: UIViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}

class RegularViewController: ProjectViewController {
    // do not neeed to override supportedInterfaceOrientations
}

class OneSpecificViewController: ProjectViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return [.portrait, .landscape]
    }
}

如果您的viewcontrollers嵌入在navigationcontroller中,您可以将其子类化为:

class CustomNavigationController: UINavigationController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        guard let topViewController = topViewController else {
            // default
            return .portrait
        }

        return topViewController.supportedInterfaceOrientations
    }
}

甚至更短......

class CustomNavigationController: UINavigationController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }
}

答案 1 :(得分:1)

只需编辑AppDelegate

即可
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {

        let navController = UIApplication.sharedApplication().keyWindow?.rootViewController
        let topController = navController.viewControllers.last
        switch topController {
        case is ProjectViewController:
            return UIInterfaceOrientationMask.Portrait
        default:
            return UIInterfaceOrientationMask.All
        }

}