swift 整个项目竖屏,某个页面横屏

来源:互联网 发布:剑侠情缘mac版 编辑:程序博客网 时间:2024/05/20 18:50

1、AppDelegate

(1)定义变量 var blockRotation: Bool = false

(2)定义方法

 

Swift代码  收藏代码
  1. func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {       
  2.        if self.blockRotation{  
  3.            return UIInterfaceOrientationMask.All  
  4.        } else {  
  5.            return UIInterfaceOrientationMask.Portrait  
  6.        }  
  7.          
  8.          
  9. }  

 

Swift代码  收藏代码
  1.   

   2、要横屏的viewController

 

 

(1)获取变量

 

Swift代码  收藏代码
  1. let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate  

(2)在viewDidLoad中修改blockRotation变量值

 

 

Java代码  收藏代码
  1. override func viewDidLoad() {  
  2.        super.viewDidLoad()  
  3.        appDelegate.blockRotation = true  
  4.         
  5.    }  

(3)viewWillAppear 设置页面横屏

 

 

Swift代码  收藏代码
  1. override func viewWillAppear(animated: Bool) {  
  2.        let value = UIInterfaceOrientation.LandscapeLeft.rawValue  
  3.        UIDevice.currentDevice().setValue(value, forKey: "orientation")  
  4. }  

(4)viewWillDisappear设置页面转回竖屏

 

 

Swift代码  收藏代码
  1. override func viewWillDisappear(animated: Bool) {  
  2.        appDelegate.blockRotation = false  
  3.        let value = UIInterfaceOrientation.Portrait.rawValue  
  4.        UIDevice.currentDevice().setValue(value, forKey: "orientation")  
  5. }  

(5)横屏页面是否支持旋转

 

Swift代码  收藏代码
  1. override func shouldAutorotate() -> Bool {  
  2.        return false  
  3. }  

 

 

0 0