iOS屏幕旋转控制,自动旋转,手动旋转,锁定屏幕
1. 设置App支持的旋转方向(2种方式)
1. 通过工程设置
General
->Deployment Info
->Device Orientation
,勾选支持的方向
2. 通过代码设置(AppDelegate)
1 2 3
| func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return .allButUpsideDown }
|
方式1存在一个问题:如果勾选了多个方向,如果横屏进入App,会出现首页横屏的情况,即使设置了VC
只支持竖屏,推荐使用方式2
2. ViewController旋转控制
通常在需要旋转的ViewController,重写下面三个方法即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| override var shouldAutorotate: Bool { return true }
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return .portrait }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .landscape }
|
3. 容器
如果ViewController在容器控制器里面的话(UINavigationController
和UITabBarController
)需要重写容器,让其指向子控制器
TabBarController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import UIKit
class TabBarController: UITabBarController { override var shouldAutorotate: Bool { return self.selectedViewController?.shouldAutorotate ?? false }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return self.selectedViewController?.supportedInterfaceOrientations ?? UIInterfaceOrientationMask.portrait }
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return self.selectedViewController?.preferredInterfaceOrientationForPresentation ?? .portrait } }
|
NavigationController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import UIKit
class NavigationController: UINavigationController { override var shouldAutorotate: Bool { return self.topViewController?.shouldAutorotate ?? false }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return self.topViewController?.supportedInterfaceOrientations ?? UIInterfaceOrientationMask.portrait }
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return self.topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait } }
|
4. 强制旋转
iOS没有提供公开的API直接修改屏幕方向,通常我们用kvc
的方式实现
1 2 3 4 5
| UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
|
5. 方向锁定
通过控制VC支持supportedInterfaceOrientations
的方向,就可以控制锁定了,只返回一种方向,就能实现锁定的功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| private var lookOrientation: UIInterfaceOrientation?
self.lookOrientation = UIApplication.shared.statusBarOrientation
self.lookOrientation = nil
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { if let orientation = self.lookOrientation { return orientation.orientationMask } else { return .allButUpsideDown } }
extension UIInterfaceOrientation { var orientationMask: UIInterfaceOrientationMask { switch self { case .unknown: return .allButUpsideDown case .portrait: return .portrait case .portraitUpsideDown: return .portraitUpsideDown case .landscapeLeft: return .landscapeLeft case .landscapeRight: return .landscapeRight @unknown default: return .allButUpsideDown } } }
|