1、UIDeviceOrientation 裝置的實體方向
-
簡介
UIDeviceOrientation即我們手持的移動裝置的Orientation,是一個三圍空間,故有六個方向:
1 2 3 4 5 6 7 | |
-
擷取
通過[UIDevice currentDevice].orientation擷取目前裝置的方向
當關閉了系統的橫豎屏切換開關,即系統層級隻允許豎屏時,再通過上述方式擷取到的裝置方向将隻是UIDeviceOrientationPortrait。
UIDeviceOrientation是硬體裝置的方向,是随着硬體自身改變的,隻能取值,不能設定。
2、UIInterfaceOrientation界面的顯示方向
-
簡介
UIInterfaceOrientation即我們看到的視圖的Orientation,可以了解為statusBar所在的方向,是一個二維空間,有四個方向:
1 2 3 4 5 | |
-
擷取
1.viewController. interfaceOrientation該方法在 iOS8之後廢除。
2.[UIApplication sharedApplication].statusBarOrientation即狀态欄的方向。
-
關聯
UIDeviceOrientation與UIInterfaceOrientation是兩個互不相幹的屬性。
其中一個并不會随另外一個變化而變化。但大多數情況下,兩者會一起出現,主要是為了達到視覺的統一。
注意:
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
兩者是相反的,但是根據官方文檔,如下所示,我們發現兩者的方向是一緻的。
DeviceOrientation.png
InterfaceOrientation.png
經測試發現如下結論。
當device處于UIInterfaceOrientationLandscapeLeft的狀态,即相當于裝置向左旋轉,要想達到視覺上的統一,頁面應該向右旋轉,即[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];其實設定完之後,再去擷取UIInterfaceOrientation發現得到的是UIInterfaceOrientationLandscapeLeft,與官方文檔并不沖突。
這裡其實是兩個概念,一是旋轉的方向,二是所處的方向,使用是要當心了!
-
擴充
UIInterfaceOrientationMask
這是ios6之後新增的一組枚舉值,使用組合時更加友善,使用時根據傳回值類型選擇正确的格式,避免可能出現的bug
1 2 3 4 5 6 7 8 9 | |
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.3 UIViewController設定
通過三個代理方法設定
1 2 3 4 5 6 7 8 9 10 11 12 | |
解釋:
1.第二個方法,在iPad上的預設傳回值是UIInterfaceOrientationMaskAll,iPhone上的預設傳回值是UIInterfaceOrientationMaskAllButUpsideDown;
2.在前面DeviceOrientation即使全部勾選了,若要iPhone支援UpsideDown,也要在viewcontroller裡重寫第二個方法。傳回包含UpsideDown的方向;
3.第三個方法,比如同時支援Portrait和Landscape方向,但想優先顯示Landscape方向,那軟體啟動的時候就會先顯示Landscape,在手機切換旋轉方向的時候仍然可以在Portrait和Landscape之間切換;
-
3.1.4 總結
1.這三種方式控制規則的交集就是一個viewController的最終支援的方向;
如果最終的交集為空,在iOS6以後會抛出UIApplicationInvalidInterfaceOrientationException崩潰異常。
2.如果關閉了系統的橫豎屏切換開關,即系統層級隻允許豎屏時,再通過上述方式擷取到的裝置方向将是UIDeviceOrientationPortrait。UIInterfaceOrientation也将不會改變。
3.第三種方式隻有在目前viewController是window的rootViewController。或者是通過presentModalViewController而顯示出來的.才會生效。作用于viewController及其childViewController。否則UIKit并不會執行上述方法。
-
3.1.5 靈活控制
上述方法基本上可以認為是一種全局設定,實際項目中可能會是一個或幾個頁面需要單獨控制。通過UIViewController的三個方法設定Orientation時,隻有在是window的rootViewController或者modal模式下才生效。且作用于其childViewController單獨設定某個viewController并沒有效果。這種情況主要可以通過下面幾種方法解決。
1.在rootViewController裡加判斷
1 2 3 4 5 6 | |
2.在UINavigationController或UITabBarController裡重寫
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
UINavigationController 使用self.topViewController
UITabBarController 使用self.selectedViewController
然後在viewController重寫這三個方法,這樣就巧妙的繞開了UIKit隻調用rootViewController的方法的規則. 把決定權交給了目前正在顯示的viewController.
但是
這樣是可以在目前viewController達到預期效果,但是在傳回上一頁時,或者在目前頁面不不支援的方向的上一頁進來時,不能立即達到預期狀态,需要裝置方向更換一次才能恢複正常。
解決方案:
1 2 3 4 5 6 | |
1 2 3 4 5 6 | |
這樣就會觸發-(BOOL)shouldAutorotate方法和
-(UIInterfaceOrientationMask)supportedInterfaceOrientations方法,但是會閃一下,依然不完美。
3.把需要單獨設定的viewController添加在一個獨立的navgationController裡
這種方法不适用rootViewController是UITabBarController的, 不推薦使用。
3.2 主動控制
所謂主動控制即不讓其自動旋轉Autorotate == NO,方向自行控制。主要有兩種方式
-
3.2.1 UIView.transform
代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
注意:
- statusBar不會自己旋轉,這裡要首先設定statusBar的方向。iOS9後setStatusBarOrientation方法廢除
- 我們這裡選擇的是self.navigationController進行旋轉,當然也可以是self.view或者self.window,都可以,最好是全屏的view.
- 我們需要顯示的設定bounds,UIKit并不知道你偷偷摸摸幹了這些事情。
- -(BOOL)shouldAutorotate方法,應傳回NO
在iOS 9 之後橫屏時,狀态欄會消失。
解決方法:確定Info.plist中的【View controller-based status bar appearance】為YES,然後重寫viewController的- (BOOL)prefersStatusBarHidden,傳回值是NO。
3.2.2 強制旋轉setOrientation
setOrientation 在iOS3以後變為私有方法了,不能直接去調用此方法,否則後果就是被打回。
不能直接調用,但是可以間接的去調用,下面的方法就是利用 KVO機制去間接調用,多次驗證不會被打回,放心!
1 2 3 4 5 6 7 8 | |