iOS9橫豎屏設置的處理方法和實例講解

在一般的視頻類APP播放的時候都會支持橫屏,這樣做的好處就是便於觀看。你的項目中支持橫屏嗎?我們一起了解一下,在iOS9中橫豎屏設置的處理方法吧!

支持橫豎屏配置

在iOS6以後,如果APP需要支持橫屏,需要在xcode設置中General裏面進行勾選配置:

orientation1

配置完成之後,我們可以看一下Info.plist裏面的Supported interface orientations選項也相應的改變了。如下圖:

orientation2

當然,我們也可以直接在Info.plist進行配置。

支持橫豎屏方法

在iOS6之前我們可以直接用這個方法進行配置:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;

在iOS6之後,這個方法被NS_DEPRECATED_IOS,也就是廢棄掉了。廢棄了這個方法,蘋果相應的也給出了新的方法來代替:

// New Autorotation support.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

我們可以看到iOS6之前是一個方法,在iOS6之後變成兩個方法了,一個是是否旋轉的方法,一個是支持的方向的方法。

實例一:

假設:我們ViewController是直接加載window的self.window.rootViewController上面的。代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}

如果我們要是想支持上面General裏勾選的方向(豎屏、橫屏向左已經橫屏向右)該如何實現呢?首先,我們應該設置讓他支持旋轉,然後在設置支持的方向。代碼如下:

//支持旋轉
-(BOOL)shouldAutorotate{
   return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

其中UIInterfaceOrientationMask是一個枚舉:

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;    

可以根據自己的需求來選擇。上面我們說了假設這個條件,如果rootViewController上導航,我們直接在ViewController裏面設置,這個方法就不靈了。(大家可以自己測試一下)

實例二:

爲什麼是導航上面的方法就不靈了呢?原因很簡單,我們沒有設置導航支持的方向。別忘了UINavigationController也是UIViewController的子類。需要受到同樣的待遇的。

如何設置呢?我們可以創建一個UINavigationController的子類,假設叫GGPublicNavigationViewController。然後,我們在GGPublicNavigationViewController.m文件裏面也實現着兩個方法:

//支持旋轉
-(BOOL)shouldAutorotate{
   return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

這樣設置之後,即使我們push進去的UIViewController沒有實現上面的連個方法,也是可以支持橫屏的。也就是說,我們push的所有都支持橫屏。這個做法是不是很暴力!

實例三:

有些童鞋會問了,如何控制每個界面支持的方向呢?這也是可以辦到的,在GGPublicNavigationViewController不能寫死支持哪個。我們可以這麼寫:

-(BOOL)shouldAutorotate{
    return [self.topViewController shouldAutorotate];
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];;
}

self.topViewController是當前導航顯示的UIViewController,這樣就可以控制每個UIViewController所支持的方向啦!

好啦,關於iOS9中橫豎屏的處理就說這麼多吧!(其實iOS7、iOS8也是這麼設置的)如果你覺得文章還不錯,分享一下吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章