【发布时间】:2019-06-27 06:03:28
【问题描述】:
我目前正在将一个 iphone 应用程序改编为 ipad,并在 viewController 中实现了以下代码,以维护其中一个横向视图(其他视图为纵向):
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
//TODO: - Keep presentationView in landscape
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewDidDisappear(_ animated: Bool) {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var shouldAutorotate: Bool {
return true
}
这适用于 iphone,但在 iPad 上显示应用程序时不起作用,即视图旋转并且可以在纵向和横向中看到。感谢任何关于我如何调整代码以使其在 iPad 上工作的建议。
【问题讨论】:
-
UIDevice.current.setValue(value, forKey: "orientation")等代码不合适。它不受支持(即使它可能在某些条件下工作)。避免需要使用私有 API 的黑客攻击。 -
感谢您的回复。 Rahul 发布的链接中的以下内容特别有用: - 确保在“目标设置”->“常规”->“部署信息”中选中“需要全屏”。如果未选中,supportedInterfaceOrientationsFor 委托将不会被调用。 -
标签: ios swift ipad viewcontroller landscape-portrait