@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var blockRotation = Bool()
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if blockRotation {
return .allButUpsideDown //支持橫豎屏旋轉(zhuǎn)
// return .landscapeLeft //強(qiáng)制橫屏?xí)r這里可保持一個(gè)方向
}
return .portrait
}
}
class NetPlayerController: BaseViewController {
//App信息
let App_Delegate = (UIApplication.shared.delegate) as! AppDelegate
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
App_Delegate.blockRotation = true
setNewOrientation(fullScreen: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
App_Delegate.blockRotation = false
//判斷退出時(shí)是否是橫屏
if UIApplication.shared.statusBarOrientation.isLandscape {
//是橫屏讓變回豎屏
setNewOrientation(fullScreen: false)
}
}
//橫豎屏
func setNewOrientation(fullScreen: Bool) {
if fullScreen { //橫屏
let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.landscapeRight.rawValue)
UIDevice.current.setValue(orientationTarget, forKey: "orientation")
} else { //豎屏
let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.portrait.rawValue)
UIDevice.current.setValue(orientationTarget, forKey: "orientation")
}
}
}