在AppDelegate中:
1.添加属性:
var isForceLandscape:Bool = true //标记是否横屏
2.重写代理方法:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if self.isForceLandscape {
return UIInterfaceOrientationMask.landscapeRight
}
return UIInterfaceOrientationMask.portrait
}
写两个全局方法:
func setHengP() {
appDelegate.isForceLandscape = true
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
func setShuP(){
appDelegate.isForceLandscape = false
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
在需要横屏 或者 竖屏的controller中 的ViewWillAppear调用相应方法;在ViewWillDisappear方法中将原来设置回来;