在iPad上锁定方向并禁用旋转

我有以下代码在iPhone上运行正常,但在iPad上运行不正常。 它应该锁定横向方向并仅为此视图控制器禁用自动旋转(不适用于整个应用程序 – 不更改project / info.plist文件中的属性):

override var shouldAutorotate : Bool { return false } 

并在viewDidLoad()

 let landscapeValue = UIInterfaceOrientation.landscapeRight.rawValue UIDevice.current.setValue(landscapeValue, forKey: "orientation") 

如何锁定横向方向并禁用当前ViewController的旋转(就像它已经完全在iPhone上工作一样)?

我的规格:

XCode – 9.1 Build 9B55

斯威夫特 – 4.0.2

在iPad Air上测试 – iOS 10.3.3

我的问题的答案实际上非常简单。 您必须在项目的Deployement Info中激活Requires full screen 。 之后,iPad的行为与使用此代码的iPhone相同。