1.在Appdelegate中:
// 是否横屏
var isLandscape = false
// MARK: 是否横屏
extension AppDelegate {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if isLandscape {
return .all
}
else {
return .portrait
}
}
}
2.在需要横屏的界面:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.isLandscape = true
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.isLandscape = false
// MARK: - 这句很重要
let unknownValue = UIInterfaceOrientation.unknown.rawValue
UIDevice.current.setValue(unknownValue, forKey: "orientation")
// 将视图还原成竖屏
let orientationTarget = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(orientationTarget, forKey: "orientation")
}
// 是否支持自转 看具体需求
override var shouldAutorotate: Bool {
return false
}
// 设置横屏方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
// 横屏 home键在右
return .landscapeRight
}
网友评论