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

支持橫豎屏配置

在iOS6之後,若是APP須要支持橫屏,須要在xcode設置中General裏面進行勾選配置:xcode

配置完成以後,咱們能夠看一下Info.plist裏面的Supported interface orientations選項也相應的改變了。以下圖:app

固然,咱們也能夠直接在Info.plist進行配置。ide

支持橫豎屏方法

在iOS6以前咱們能夠直接用這個方法進行配置:測試

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

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

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

咱們能夠看到iOS6以前是一個方法,在iOS6以後變成兩個方法了,一個是是否旋轉的方法,一個是支持的方向的方法。code

實例一:

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

- (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裏勾選的方向(豎屏、橫屏向左已經橫屏向右)該如何實現呢?首先,咱們應該設置讓他支持旋轉,而後在設置支持的方向。代碼以下:io

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

其中UIInterfaceOrientationMask是一個枚舉:import

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中橫豎屏的處理就說這麼多吧!(其實iOS七、iOS8也是這麼設置的)若是你以爲文章還不錯,分享一下吧!

相關文章
相關標籤/搜索