ios - 在纵向状态下强制旋转 UIViewController

标签 ios objective-c uiviewcontroller landscape device-orientation

在这样的环境下 (修复项目配置中支撑方向为纵向)

enter image description here

你能在 Landscape 中显示一个特定的 UIViewController 吗?

最佳答案

简短的回答是你不能,因为根据 Apple 文档 here

The system intersects the view controller's supported orientations with the app's supported orientations (as determined by the Info.plist file or the app delegate's application(_:supportedInterfaceOrientationsFor:) method) and the device's supported orientations to determine whether to rotate.

要实现您所需要的,您应该将允许的方向也设置为横向,然后在 View Controller 中实现以下内容以允许纵向或横向方向(或两者):

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait//or UIInterfaceOrientationMask.landscape
  }

然后,要强制指定特定方向,您可以将方向设置为 UIDevice:

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

关于ios - 在纵向状态下强制旋转 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52176134/

相关文章:

ios - Swift - 将字符串从 VC1 传递到 VC2 - 崩溃

iphone - 具有分配属性的属性委托(delegate)的现有实例变量委托(delegate)必须是 unsafe_unretained

ios - 使用属性动态初始化任何 Objective-C 类

ios - 自定义表格单元格重用/出队

ios - Collection View 拖放延迟

ios - 我无法从模型类中的 NSDictionary 获取值?

IOS,从应用程序目录加载文件

ios - 在 View Controller 之间传递数据

iOS 13.3.1 错误 "dyld: Library not loaded:...Reason: no suitable image found. Did find:..."应用程序崩溃 [swift][xcode]

ios - Swift - 调用初始值设定项,缺少参数