天天看點

iOS 項目中,特定頁面強制橫屏

一般情況下,一個項目要麼可以橫屏要麼不可以橫屏,但是也有些APP不是這樣的,整個項目隻支援豎屏但是需要加載遊戲界面或者是必須橫屏播放視訊之類的,就要強制把某些頁面設定為橫屏:

首先要在我們項目的AppDelegate.h裡邊定義一個變量來設定螢幕的橫屏或是豎屏

@property(nonatomic,assign)NSInteger rotation_Style;

然後在AppDelegate.m裡邊重寫方法:

 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

 {

       if (_rotation_Style == 1) {//如果是1就讓螢幕強制橫屏

               return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;

             }

        else

             {

                    return (UIInterfaceOrientationMaskPortrait);

             }

 }

然後在需要強制橫屏的頁面導入AppDelegate.h頭檔案

然後兩句代碼:

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    appDelegate.rotation_Style = 1;

搞定!

當然,如果就這樣,那麼會出問題的,就是你傳回上個頁面,上個頁面就會抽風一樣也是橫屏了:

是以在傳回的方法中也加上兩句代碼:

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    appDelegate.rotation_Style = 3;//這裡可以随便寫,隻要不是1