iOS 屏幕旋轉問題總結

1、UIDeviceOrientation 設備的物理方向

  • 簡介
    UIDeviceOrientation即我們手持的移動設備的Orientation,是一個三圍空間,故有六個方向:
  UIDeviceOrientationUnknown,
  UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
  UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
  UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
  UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
  UIDeviceOrientationFaceUp,              // Device oriented flat, face up
  UIDeviceOrientationFaceDown             // Device oriented flat, face down
  • 獲取
    通過[UIDevice currentDevice].orientation獲取當前設備的方向
    當關閉了系統的橫豎屏切換開關,即系統層級只允許豎屏時,再通過上述方式獲取到的設備方向將只是UIDeviceOrientationPortrait

UIDeviceOrientation是硬件設備的方向,是隨著硬件自身改變的,只能取值,不能設置。

2、UIInterfaceOrientation界面的顯示方向

  • 簡介
    UIInterfaceOrientation即我們看到的視圖的Orientation,可以理解為statusBar所在的方向,是一個二維空間,有四個方向:
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
  • 獲取
    1.viewController. interfaceOrientation該方法在 iOS8之后廢除。
    2.[UIApplication sharedApplication].statusBarOrientation即狀態欄的方向。

  • 關聯
    UIDeviceOrientationUIInterfaceOrientation是兩個互不相干的屬性。
    其中一個并不會隨另外一個變化而變化。但大多數情況下,兩者會一起出現,主要是為了達到視覺的統一。
    注意:
    UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
    兩者是相反的,但是根據官方文檔,如下所示,我們發現兩者的方向是一致的。

    DeviceOrientation.png

    InterfaceOrientation.png

經測試發現如下結論。
當device處于UIInterfaceOrientationLandscapeLeft的狀態,即相當于設備向左旋轉,要想達到視覺上的統一,頁面應該向右旋轉,即[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];其實設置完之后,再去獲取UIInterfaceOrientation發現得到的是UIInterfaceOrientationLandscapeLeft,與官方文檔并不矛盾。
這里其實是兩個概念,一是旋轉的方向,二是所處的方向,使用是要當心了!

  • 擴展
    UIInterfaceOrientationMask
    這是ios6之后新增的一組枚舉值,使用組合時更加方便,使用時根據返回值類型選擇正確的格式,避免可能出現的bug
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),
}

3、UIInterfaceOrientation的控制

3.1 被動控制

所謂的被動控制,即支持自動旋轉Autorotate,UIInterfaceOrientation會隨著UIDeviceOrientation的改變而自動改變,以達到視覺上的統一。是系統自動控制的,我們只能控制其能夠支持自動旋轉的方向,使其在有些方向上可以跟隨旋轉,有些方向上不能。主要有以下三種方式

  • 3.1.1 【Targets】中設置
    【General】 -->【Deployment Info】-->【Device Orientation】
    Device Orientation.png

    這里雖然命名為DeviceOrientation,但實際上這里表示其界面支持的自動旋轉的方向。
    為什么這么說呢,因為這個地方的設置的值和info.plist文件里Supported interface orientations值是同步的,修改其中一個,另一個也會隨之改變。另外,不論我們這里怎么勾選,對程序當中獲取當前設備的orientation是沒有影響的。
Supported interface orientations.png
  • 3.1.2 UIWindow設置
    iOS6的UIApplicationDelegate提供了下述方法,能夠指定UIWindow中的界面的屏幕方向:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window NS_AVAILABLE_IOS(6_0);
    該方法默認值為Info.plist中配置的Supported interface orientations項的值。

  • 3.1.3UIViewController設置
    通過三個代理方法設置

//Interface的方向是否會跟隨設備方向自動旋轉,如果返回NO,后兩個方法不會再調用
- (BOOL)shouldAutorotate {
    return YES;
}
//返回直接支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
//返回最優先顯示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

解釋:
1.第二個方法,在iPad上的默認返回值是UIInterfaceOrientationMaskAll,iPhone上的默認返回值是UIInterfaceOrientationMaskAllButUpsideDown
2.在前面DeviceOrientation即使全部勾選了,若要iPhone支持UpsideDown,也要在viewcontroller里重寫第二個方法。返回包含UpsideDown的方向;
3.第三個方法,比如同時支持PortraitLandscape方向,但想優先顯示Landscape方向,那軟件啟動的時候就會先顯示Landscape,在手機切換旋轉方向的時候仍然可以在PortraitLandscape之間切換;

  • 3.1.4 總結
    1.這三種方式控制規則的交集就是一個viewController的最終支持的方向;
    如果最終的交集為空,在iOS6以后會拋出UIApplicationInvalidInterfaceOrientationException崩潰異常。
    2.如果關閉了系統的橫豎屏切換開關,即系統層級只允許豎屏時,再通過上述方式獲取到的設備方向將是UIDeviceOrientationPortraitUIInterfaceOrientation也將不會改變。
    3.第三種方式只有在當前viewControllerwindowrootViewController。或者是通過presentModalViewController而顯示出來的.才會生效。作用于viewController及其childViewController。否則UIKit并不會執行上述方法。

  • 3.1.5 靈活控制
    上述方法基本上可以認為是一種全局設置,實際項目中可能會是一個或幾個頁面需要單獨控制。通過UIViewController的三個方法設置Orientation時,只有在是windowrootViewController或者modal模式下才生效。且作用于其childViewController單獨設置某個viewController并沒有效果。這種情況主要可以通過下面幾種方法解決。

  1. 在rootViewController里加判斷
  - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    if([[self topViewController] isKindOfClass:[subViewController class]])  
        return UIInterfaceOrientationMaskAllButUpsideDown;  
    else  
        return UIInterfaceOrientationMaskPortrait;
  }
  1. UINavigationControllerUITabBarController里重寫

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return self.selectedViewController.supportedInterfaceOrientations;
}
-(BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

UINavigationController 使用self.topViewController
UITabBarController 使用self.selectedViewController

然后在viewController重寫這三個方法,這樣就巧妙的繞開了UIKit只調用rootViewController的方法的規則. 把決定權交給了當前正在顯示的viewController.

但是

這樣是可以在當前viewController達到預期效果,但是在返回上一頁時,或者在當前頁面不不支持的方向的上一頁進來時,不能立即達到預期狀態,需要設備方向更換一次才能恢復正常。

解決方案:

#pragma mark -UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self presentViewController:[UIViewController new] animated:NO completion:^{
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
#pragma mark -UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self presentViewController:[UIViewController new] animated:NO completion:^{
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

這樣就會觸發-(BOOL)shouldAutorotate方法和
-(UIInterfaceOrientationMask)supportedInterfaceOrientations方法,但是會閃一下,依然不完美。

  1. 把需要單獨設置的viewController添加在一個獨立的navgationController
    這種方法不適用rootViewControllerUITabBarController的, 不推薦使用。
3.2 主動控制

所謂主動控制即不讓其自動旋轉Autorotate == NO,方向自行控制。主要有兩種方式

  • 3.2.1 UIView.transform
    代碼如下:
//設置statusBar
[[UIApplication sharedApplication] setStatusBarOrientation:orientation];

//計算旋轉角度
float arch;
if (orientation == UIInterfaceOrientationLandscapeLeft)
    arch = -M_PI_2;
else if (orientation == UIInterfaceOrientationLandscapeRight)
    arch = M_PI_2;
else
    arch = 0;

//對navigationController.view 進行強制旋轉
self.navigationController.view.transform = CGAffineTransformMakeRotation(arch);
self.navigationController.view.bounds = UIInterfaceOrientationIsLandscape(orientation) ? CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH) : initialBounds;

注意:

  1. statusBar不會自己旋轉,這里要首先設置statusBar的方向。iOS9后setStatusBarOrientation方法廢除
  2. 我們這里選擇的是self.navigationController進行旋轉,當然也可以是self.view或者self.window,都可以,最好是全屏的view.
  3. 我們需要顯示的設置bounds,UIKit并不知道你偷偷摸摸干了這些事情。
  4. -(BOOL)shouldAutorotate方法,應返回NO

在iOS 9 之后橫屏時,狀態欄會消失。

解決方法:確保Info.plist中的【View controller-based status bar appearance】YES,然后重寫viewController- (BOOL)prefersStatusBarHidden,返回值是NO。詳細參考iOS-UIStatusBar詳細總結

  • 3.2.2 強制旋轉setOrientation
    setOrientation 在iOS3以后變為私有方法了,不能直接去調用此方法,否則后果就是被打回。
    不能直接調用,但是可以間接的去調用,下面的方法就是利用 KVO機制去間接調用,多次驗證不會被打回,放心!
-(void)viewWillAppear:(BOOL)animated{
//首先設置UIInterfaceOrientationUnknown欺騙系統,避免可能出現直接設置無效的情況
    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

參考文章:
iOS屏幕旋轉學習筆記
[iOS]Orientation 想怎么轉就怎么轉
iOS 知識小集(橫豎屏切換)
iOS強制改變物理設備方向的進階方法

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,967評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,273評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,870評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,742評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,527評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,010評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,108評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,250評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,769評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,656評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,853評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,371評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,103評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,472評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,717評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,487評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,815評論 2 372

推薦閱讀更多精彩內容