禁止旋转屏幕,有两种方法
1、直接Xcode写死,简单方便 (不推荐)
但是这样有个问题,当需要旋转屏幕或者,在倒入某些第三方的时候,如果不支持屏幕旋转,在使用第三方的时候应用会蹦。所以,这样写-不推荐
2、用代码控制屏幕旋转与否
// 此方法用来设置是否让页面支持自动旋转屏幕,return YES既可以自动旋转屏幕。NO就不能自动旋转屏幕
override var shouldAutorotate: Bool
override var supportedInterfaceOrientations: UIInterfaceOrientationMask
/*
此方法用来设置当前页面支持的屏幕方向
UIInterfaceOrientationMask是一个枚举:
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),//向上为正方向的竖屏
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),//向左移旋转的横屏
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),//向右旋转的横屏
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),//向下为正方向的竖屏
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),//向左或者向右的横屏
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),//所有的横竖屏方向都支持
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),//支持向上的竖屏和左右方向的横屏
*/
这两个方法就可以控制屏幕的旋转和不旋转了
a、在Appdelegate中设置三个属性
var isForceLandscape:Bool = false
var isForcePortrait:Bool = false
var isForceAllDerictions:Bool = false //支持所有方向
然后重写 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)
/// 设置屏幕支持的方向
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if isForceAllDerictions == true {
return .all
} else if isForceLandscape == true {
return .landscape
} else if isForcePortrait == true {
return .portrait
}
return .portrait
}
到这儿,就已经可以禁止屏幕旋转了
b、强制旋转横屏
在需要强制旋转屏幕的VC中添加以下代码就可以实现屏幕强制横屏
/// 屏幕旋转
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//self.setNavigationBarHidden(hidden: true)
forceOrientationLandscape()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//self.setNavigationBarHidden(hidden: false)
forceOrientationPortrait()
}
override var shouldAutorotate: Bool {
get {
return true
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .landscapeRight
}
}
// 强制旋转横屏
func forceOrientationLandscape() {
let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.isForceLandscape = true
appdelegate.isForcePortrait = false
_ = appdelegate.application(UIApplication.shared, supportedInterfaceOrientationsFor: view.window)
let oriention = UIInterfaceOrientation.landscapeRight // 设置屏幕为横屏
UIDevice.current.setValue(oriention.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
// 强制旋转竖屏
func forceOrientationPortrait() {
let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.isForceLandscape = false
appdelegate.isForcePortrait = true
_ = appdelegate.application(UIApplication.shared, supportedInterfaceOrientationsFor: view.window)
let oriention = UIInterfaceOrientation.portrait // 设置屏幕为竖屏
UIDevice.current.setValue(oriention.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
这样当进入当前VC的时候就可以实现横屏,退出当前VC为竖屏
由于时间关系,目前就先写这么多,后续再更新