個別頁面設(shè)置橫屏?xí)r出現(xiàn)了Bug,這里就我遇到的情況做個說明。
iOS豎屏狀態(tài)下present一個橫屏的viewController(繼承BaseViewController)出現(xiàn)bug,每次app第一次啟動后,會出現(xiàn)如附件中的現(xiàn)象,本應(yīng)該橫屏全屏的界面,結(jié)果成了豎屏只有上半邊的情況,下半邊全黑,再次進入這個頁面就不會再出現(xiàn)。

圖1.png
這時我在【General】【Device Orientation】中只選擇了【Portrait】

圖2.png
在需要橫屏的ViewController中添加代碼:
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeRight
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeRight
}
解決辦法
在【General】【Device Orientation】中選擇了【Portrait】和【Landscape Right】,只在AppDelegate中設(shè)置
var allowRotation: Bool = false //是否允許轉(zhuǎn)向
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if self.allowRotation {
return .landscapeRight
} else {
return .portrait
}
}
然后在需要設(shè)置橫屏的Controller 的 viewDidLoad 函數(shù)中設(shè)置
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.allowRotation = true
}
在點擊返回按鈕時重新設(shè)置上面的值
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.allowRotation = false
}