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即狀態欄的方向。

關聯

UIDeviceOrientation與UIInterfaceOrientation是兩個互不相干的屬性。

其中一個并不會隨另外一個變化而變化。但大多數情況下,兩者會一起出現,主要是為了達到視覺的統一。

注意:

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.3 UIViewController設置

通過三個代理方法設置

//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.第三個方法,比如同時支持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里加判斷

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

if([[self topViewController] isKindOfClass:[subViewController class]])?

return UIInterfaceOrientationMaskAllButUpsideDown;?

else

return UIInterfaceOrientationMaskPortrait;

}

2.在UINavigationController或UITabBarController里重寫

-(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方法,但是會閃一下,依然不完美。

3.把需要單獨設置的viewController添加在一個獨立的navgationController里

這種方法不適用rootViewController是UITabBarController的, 不推薦使用。

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;

注意:

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機制去間接調用,多次驗證不會被打回,放心!

-(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"];

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容