1、UIDeviceOrientation
設(shè)備的物理方向
- 簡(jiǎn)介
UIDeviceOrientation
即我們手持的移動(dòng)設(shè)備的Orientation
,是一個(gè)三圍空間磅废,故有六個(gè)方向:
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
獲取當(dāng)前設(shè)備的方向
當(dāng)關(guān)閉了系統(tǒng)的橫豎屏切換開關(guān)题翰,即系統(tǒng)層級(jí)只允許豎屏?xí)r,再通過上述方式獲取到的設(shè)備方向?qū)⒅皇?code>UIDeviceOrientationPortrait窖张。
UIDeviceOrientation
是硬件設(shè)備的方向幕随,是隨著硬件自身改變的,只能取值宿接,不能設(shè)置赘淮。
2、UIInterfaceOrientation
界面的顯示方向
- 簡(jiǎn)介
UIInterfaceOrientation
即我們看到的視圖的Orientation
睦霎,可以理解為statusBar
所在的方向梢卸,是一個(gè)二維空間,有四個(gè)方向:
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
獲取
1.viewController. interfaceOrientation
該方法在 iOS8之后廢除副女。
2.[UIApplication sharedApplication].statusBarOrientation
即狀態(tài)欄的方向蛤高。-
關(guān)聯(lián)
UIDeviceOrientation
與UIInterfaceOrientation
是兩個(gè)互不相干的屬性。
其中一個(gè)并不會(huì)隨另外一個(gè)變化而變化碑幅。但大多數(shù)情況下戴陡,兩者會(huì)一起出現(xiàn),主要是為了達(dá)到視覺的統(tǒng)一沟涨。
注意:
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight
,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
兩者是相反的恤批,但是根據(jù)官方文檔,如下所示裹赴,我們發(fā)現(xiàn)兩者的方向是一致的喜庞。
經(jīng)測(cè)試發(fā)現(xiàn)如下結(jié)論。
當(dāng)device處于UIInterfaceOrientationLandscapeLeft
的狀態(tài)篮昧,即相當(dāng)于設(shè)備向左旋轉(zhuǎn)赋荆,要想達(dá)到視覺上的統(tǒng)一,頁(yè)面應(yīng)該向右旋轉(zhuǎn)懊昨,即[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];
其實(shí)設(shè)置完之后窄潭,再去獲取UIInterfaceOrientation
發(fā)現(xiàn)得到的是UIInterfaceOrientationLandscapeLeft
,與官方文檔并不矛盾。
這里其實(shí)是兩個(gè)概念,一是旋轉(zhuǎn)的方向嫉你,二是所處的方向月帝,使用是要當(dāng)心了!
- 擴(kuò)展
UIInterfaceOrientationMask
這是ios6之后新增的一組枚舉值幽污,使用組合時(shí)更加方便嚷辅,使用時(shí)根據(jù)返回值類型選擇正確的格式,避免可能出現(xiàn)的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 被動(dòng)控制
所謂的被動(dòng)控制簸搞,即支持自動(dòng)旋轉(zhuǎn)Autorotate
,UIInterfaceOrientation會(huì)隨著UIDeviceOrientation的改變而自動(dòng)改變准潭,以達(dá)到視覺上的統(tǒng)一趁俊。是系統(tǒng)自動(dòng)控制的,我們只能控制其能夠支持自動(dòng)旋轉(zhuǎn)的方向刑然,使其在有些方向上可以跟隨旋轉(zhuǎn)寺擂,有些方向上不能。主要有以下三種方式
- 3.1.1 【Targets】中設(shè)置
【General】
-->【Deployment Info】
-->【Device Orientation】
這里雖然命名為DeviceOrientation
,但實(shí)際上這里表示其界面支持的自動(dòng)旋轉(zhuǎn)的方向泼掠。
為什么這么說呢怔软,因?yàn)檫@個(gè)地方的設(shè)置的值和info.plist
文件里Supported interface orientations
值是同步的,修改其中一個(gè)择镇,另一個(gè)也會(huì)隨之改變挡逼。另外,不論我們這里怎么勾選沐鼠,對(duì)程序當(dāng)中獲取當(dāng)前設(shè)備的orientation
是沒有影響的挚瘟。
-
3.1.2
UIWindow
設(shè)置
iOS6的UIApplicationDelegate
提供了下述方法叹谁,能夠指定UIWindow
中的界面的屏幕方向:- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window NS_AVAILABLE_IOS(6_0);
該方法默認(rèn)值為Info.plist
中配置的Supported interface orientations
項(xiàng)的值饲梭。 3.1.3
UIViewController
設(shè)置
通過三個(gè)代理方法設(shè)置
//Interface的方向是否會(huì)跟隨設(shè)備方向自動(dòng)旋轉(zhuǎn),如果返回NO,后兩個(gè)方法不會(huì)再調(diào)用
- (BOOL)shouldAutorotate {
return YES;
}
//返回直接支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
//返回最優(yōu)先顯示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
解釋:
1.第二個(gè)方法焰檩,在iPad上的默認(rèn)返回值是UIInterfaceOrientationMaskAll
憔涉,iPhone上的默認(rèn)返回值是UIInterfaceOrientationMaskAllButUpsideDown
;
2.在前面DeviceOrientation
即使全部勾選了析苫,若要iPhone支持UpsideDown
兜叨,也要在viewcontroller
里重寫第二個(gè)方法。返回包含UpsideDown
的方向衩侥;
3.第三個(gè)方法国旷,比如同時(shí)支持Portrait
和Landscape
方向,但想優(yōu)先顯示Landscape
方向茫死,那軟件啟動(dòng)的時(shí)候就會(huì)先顯示Landscape
跪但,在手機(jī)切換旋轉(zhuǎn)方向的時(shí)候仍然可以在Portrait
和Landscape
之間切換;
3.1.4 總結(jié)
1.這三種方式控制規(guī)則的交集就是一個(gè)viewController
的最終支持的方向峦萎;
如果最終的交集為空屡久,在iOS6以后會(huì)拋出UIApplicationInvalidInterfaceOrientationException
崩潰異常忆首。
2.如果關(guān)閉了系統(tǒng)的橫豎屏切換開關(guān),即系統(tǒng)層級(jí)只允許豎屏?xí)r被环,再通過上述方式獲取到的設(shè)備方向?qū)⑹?code>UIDeviceOrientationPortrait糙及。UIInterfaceOrientation
也將不會(huì)改變。
3.第三種方式只有在當(dāng)前viewController
是window
的rootViewController
筛欢〗牵或者是通過presentModalViewController
而顯示出來的.才會(huì)生效。作用于viewController
及其childViewController
版姑。否則UIKit
并不會(huì)執(zhí)行上述方法揣钦。3.1.5 靈活控制
上述方法基本上可以認(rèn)為是一種全局設(shè)置,實(shí)際項(xiàng)目中可能會(huì)是一個(gè)或幾個(gè)頁(yè)面需要單獨(dú)控制漠酿。通過UIViewController
的三個(gè)方法設(shè)置Orientation
時(shí)冯凹,只有在是window
的rootViewController
或者modal
模式下才生效。且作用于其childViewController
單獨(dú)設(shè)置某個(gè)viewController
并沒有效果炒嘲。這種情況主要可以通過下面幾種方法解決宇姚。
- 在rootViewController里加判斷
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
if([[self topViewController] isKindOfClass:[subViewController class]])
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskPortrait;
}
- 在
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
重寫這三個(gè)方法,這樣就巧妙的繞開了UIKit
只調(diào)用rootViewController
的方法的規(guī)則. 把決定權(quán)交給了當(dāng)前正在顯示的viewController
.
但是
這樣是可以在當(dāng)前viewController
達(dá)到預(yù)期效果夫凸,但是在返回上一頁(yè)時(shí)浑劳,或者在當(dāng)前頁(yè)面不不支持的方向的上一頁(yè)進(jìn)來時(shí),不能立即達(dá)到預(yù)期狀態(tài)夭拌,需要設(shè)備方向更換一次才能恢復(fù)正常魔熏。
解決方案:
#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];
}];
}
這樣就會(huì)觸發(fā)-(BOOL)shouldAutorotate
方法和
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
方法,但是會(huì)閃一下鸽扁,依然不完美蒜绽。
- 把需要單獨(dú)設(shè)置的
viewController
添加在一個(gè)獨(dú)立的navgationController
里
這種方法不適用rootViewController
是UITabBarController
的, 不推薦使用。
3.2 主動(dòng)控制
所謂主動(dòng)控制即不讓其自動(dòng)旋轉(zhuǎn)Autorotate == NO
,方向自行控制桶现。主要有兩種方式
- 3.2.1 UIView.transform
代碼如下:
//設(shè)置statusBar
[[UIApplication sharedApplication] setStatusBarOrientation:orientation];
//計(jì)算旋轉(zhuǎn)角度
float arch;
if (orientation == UIInterfaceOrientationLandscapeLeft)
arch = -M_PI_2;
else if (orientation == UIInterfaceOrientationLandscapeRight)
arch = M_PI_2;
else
arch = 0;
//對(duì)navigationController.view 進(jìn)行強(qiáng)制旋轉(zhuǎn)
self.navigationController.view.transform = CGAffineTransformMakeRotation(arch);
self.navigationController.view.bounds = UIInterfaceOrientationIsLandscape(orientation) ? CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH) : initialBounds;
注意:
statusBar
不會(huì)自己旋轉(zhuǎn)躲雅,這里要首先設(shè)置statusBar
的方向。iOS9后setStatusBarOrientation
方法廢除- 我們這里選擇的是
self.navigationController
進(jìn)行旋轉(zhuǎn)骡和,當(dāng)然也可以是self.view
或者self.window
,都可以相赁,最好是全屏的view
. - 我們需要顯示的設(shè)置
bounds
,UIKit
并不知道你偷偷摸摸干了這些事情。 -(BOOL)shouldAutorotate
方法慰于,應(yīng)返回NO
在iOS 9 之后橫屏?xí)r钮科,狀態(tài)欄會(huì)消失。
解決方法:確保
Info.plist
中的【View controller-based status bar appearance】
為YES
婆赠,然后重寫viewController
的- (BOOL)prefersStatusBarHidden
绵脯,返回值是NO
。詳細(xì)參考iOS-UIStatusBar詳細(xì)總結(jié)
- 3.2.2 強(qiáng)制旋轉(zhuǎn)
setOrientation
setOrientation
在iOS3以后變?yōu)樗接蟹椒耍荒苤苯尤フ{(diào)用此方法桨嫁,否則后果就是被打回植兰。
不能直接調(diào)用,但是可以間接的去調(diào)用璃吧,下面的方法就是利用 KVO機(jī)制去間接調(diào)用楣导,多次驗(yàn)證不會(huì)被打回,放心畜挨!
-(void)viewWillAppear:(BOOL)animated{
//首先設(shè)置UIInterfaceOrientationUnknown欺騙系統(tǒng)筒繁,避免可能出現(xiàn)直接設(shè)置無效的情況
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
參考文章:
iOS屏幕旋轉(zhuǎn)學(xué)習(xí)筆記
[iOS]Orientation 想怎么轉(zhuǎn)就怎么轉(zhuǎn)
iOS 知識(shí)小集(橫豎屏切換)
iOS強(qiáng)制改變物理設(shè)備方向的進(jìn)階方法