【问题标题】:Lock all screen orientations to portrait except for 1将所有屏幕方向锁定为纵向,除了 1
【发布时间】:2017-03-20 12:50:23
【问题描述】:

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

除了我正在使用的 1 个框架(我的地图中的 80 个屏幕中的 1 个)需要横向支持。因此,我不得不在我的 plist 中允许它。

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

我的项目的一个优点是所有其他 ViewController 都继承自 ProjectViewController。

首选 Swift 中的答案。

【问题讨论】:

    标签: ios objective-c iphone swift


    【解决方案1】:
    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]
        }
    }
    

    如果您的视图控制器嵌入在导航控制器中,您可以像这样子类化它:

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

    【讨论】:

    • ProjectViewControllers 似乎仍以横向模式打开
    • 那我猜它是嵌入到导航控制器之类的?
    • 是的,没错
    【解决方案2】:

    只需编辑 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
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多