logo头像
Snippet 博客主题

屏幕旋转

  1. 项目不能横屏但是某些页面如视频播放需要横屏
    需要勾选横竖屏
    image.png

在AppDelegate中全局设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var isForceLandscape:Bool = false
var isForcePortrait:Bool = false
var isForceAllDerictions:Bool = false //支持所有方向


//设置屏幕旋转
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
}

在控制器中调用这两个方法来强制旋转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 强制旋转横屏
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()
}
微信打赏

赞赏是不耍流氓的鼓励