![](/img/trans.png)
[英]Swift: Enable landscape for one controller and for the rest only portrait
[英]Enable portrait and landscape for one view controller and only portrait for other view controllers in swift?
我的應用程序中有10個視圖控制器,我只想在縱向和橫向模式下查看一個視圖控制器,其余的只能在縱向模式下查看。 注意:我正在使用UINavigationController。 任何幫助表示贊賞!
我有同樣的問題,但找到了解決方案:
首先,在基本設置中允許應用程序的橫向模式。 然后修改你的AppDelegate:
internal var shouldRotate = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
對於應允許橫向模式的每個Controller,將以下代碼添加到viewDidLoad():
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
這允許整個應用程序以橫向模式顯示。 但它應該只在這個控制器中,所以在viewWillDissappear()中添加以下代碼,將AppDelegate中的shouldRotate var設置為false:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.