1、AppDelegate
(1)定义变量 var blockRotation: Bool = false
(2)定义方法
- func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
- if self.blockRotation{
- return UIInterfaceOrientationMask.All
- } else {
- return UIInterfaceOrientationMask.Portrait
- }
- }
2、要横屏的viewController
(1)获取变量
- let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
(2)在viewDidLoad中修改blockRotation变量值
- override func viewDidLoad() {
- super.viewDidLoad()
- appDelegate.blockRotation = true
- }
(3)viewWillAppear 设置页面横屏
- override func viewWillAppear(animated: Bool) {
- let value = UIInterfaceOrientation.LandscapeLeft.rawValue
- UIDevice.currentDevice().setValue(value, forKey: "orientation")
- }
(4)viewWillDisappear设置页面转回竖屏
- override func viewWillDisappear(animated: Bool) {
- appDelegate.blockRotation = false
- let value = UIInterfaceOrientation.Portrait.rawValue
- UIDevice.currentDevice().setValue(value, forKey: "orientation")
- }
(5)横屏页面是否支持旋转
- override func shouldAutorotate() -> Bool {
- return false
- }