關(guān)于橫豎屏相關(guān)方法的響應(yīng)都是迷之存在,很難琢磨峦甩,搞的一頭霧水藏鹊。最近項(xiàng)目中正好遇到某個(gè)控制器需要橫屏展示,查閱WWDC資料未發(fā)現(xiàn)關(guān)于orientation或者rotation相關(guān)資料魄咕。
只能通過(guò)實(shí)驗(yàn)與猜測(cè)大致了解其生命周期了。
首先蚌父,當(dāng)前屏幕是否支持橫豎屏旋轉(zhuǎn)取決于當(dāng)前window的支持方向哮兰。
設(shè)置window橫豎屏的地方有兩處,
第一處苟弛,通過(guò)工程->General->DeviceOrientation來(lái)勾選支持的方向
第二處喝滞,通過(guò)重寫(xiě)Appdelegate中的方法來(lái)設(shè)置當(dāng)前window支持的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
那么問(wèn)題來(lái)了,既然有兩處都可以設(shè)置膏秫,那么最終取值以哪一個(gè)為準(zhǔn)呢右遭?
通過(guò)測(cè)試發(fā)現(xiàn),如果兩處都設(shè)置了荔睹,最終以第二種設(shè)置為(主要)判斷依據(jù)狸演;如果第二處未設(shè)置,則以第一處為準(zhǔn)僻他。
只有當(dāng)前window支持某個(gè)方向的旋轉(zhuǎn)宵距,才能對(duì)控制器(如UIviewcontroller)進(jìn)行相應(yīng)方向的旋轉(zhuǎn)并達(dá)到想要的效果(內(nèi)容跟著轉(zhuǎn)動(dòng))。
設(shè)置了可旋轉(zhuǎn)的方向之后吨拗,就要對(duì)個(gè)別控制器做更細(xì)致的限制了满哪,比如想讓A控制只能豎屏,或者讓B控制器只能橫屏劝篷,就需要用到VC的擴(kuò)展方法(系統(tǒng)自帶的)
//是否允許旋轉(zhuǎn)
- (BOOL)shouldAutorotate {
NSLog(@"%s",__FUNCTION__);
return YES;
}
//present時(shí)可支持的旋轉(zhuǎn)方向(present時(shí)在supportedInterfaceOrientations前執(zhí)行)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(@"%s",__FUNCTION__);
if (_orientationMask & UIInterfaceOrientationMaskLandscapeLeft) {
return UIInterfaceOrientationLandscapeLeft;
}
return UIInterfaceOrientationPortrait;
}
//支持的旋轉(zhuǎn)方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSLog(@"%s",__FUNCTION__);
if ([AppDelegate sharedInstance].supportLandscape) {
return _orientationMask | UIInterfaceOrientationMaskLandscapeLeft;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
旋轉(zhuǎn)事件的傳遞順序研究
通過(guò)創(chuàng)建一個(gè)新項(xiàng)目哨鸭,頁(yè)面布局如下:
分別自定義了UINavigationcontroller和UIviewcontroller,并重寫(xiě)了vc旋轉(zhuǎn)相關(guān)的三個(gè)擴(kuò)展方法娇妓,將UINavigationcontroller設(shè)置為window的rootviewcontroller
//appdelegate中添加的代碼如下
@property(nonatomic, assign, getter=isSupportLandscape) BOOL supportLandscape;
+ (AppDelegate *)sharedInstance;
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSLog(@"%s",__FUNCTION__);
if (self.isSupportLandscape) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
//自定義的nav
- (BOOL)shouldAutorotate {
NSLog(@"%s",__FUNCTION__);
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(@"%s",__FUNCTION__);
if ([AppDelegate sharedInstance].supportLandscape) {
return UIInterfaceOrientationLandscapeLeft;
}
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSLog(@"%s",__FUNCTION__);
return UIInterfaceOrientationMaskAllButUpsideDown;
}
//自定義的vc
- (BOOL)shouldAutorotate {
NSLog(@"%s",__FUNCTION__);
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(@"%s",__FUNCTION__);
if ([AppDelegate sharedInstance].supportLandscape) {
return UIInterfaceOrientationLandscapeLeft;
}
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSLog(@"%s",__FUNCTION__);
return UIInterfaceOrientationMaskAllButUpsideDown;
}
直接啟動(dòng)APP查看旋轉(zhuǎn)方法調(diào)用情況如下:
2020-08-12 18:54:38.481 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.481 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.482 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.482 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.482 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.483 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.483 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.483 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.496 rotation[17319:3924945] -[NavVC viewWillAppear:]
2020-08-12 18:54:38.498 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.498 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.499 rotation[17319:3924945] -[NavVC shouldAutorotate]
2020-08-12 18:54:38.546 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.546 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.547 rotation[17319:3924945] -[NavVC shouldAutorotate]
2020-08-12 18:54:38.547 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.547 rotation[17319:3924945] -[NavVC supportedInterfaceOrientations]
2020-08-12 18:54:38.583 rotation[17319:3924945] -[ViewController viewWillAppear:]
2020-08-12 18:54:38.585 rotation[17319:3924945] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 18:54:38.585 rotation[17319:3924945] -[ViewController supportedInterfaceOrientations]
通過(guò)以上打印結(jié)果可以看出像鸡,APP啟動(dòng)時(shí),調(diào)用順序?yàn)锳PPdelegate的application:supportedInterfaceOrientationsForWindow: -> nav的supportedInterfaceOrientations -> nav的shouldAutorotate -> vc的supportedInterfaceOrientations
通過(guò)以上結(jié)果論證得出結(jié)論:APP在監(jiān)聽(tīng)到旋轉(zhuǎn)時(shí)哈恰,會(huì)首先通知window 并執(zhí)行delegate中的supportedInterfaceOrientationsForWindow方法只估,然后通知rootviewcontroller并執(zhí)行supportedInterfaceOrientations方法,最后通知棧頂vc并執(zhí)行supportedInterfaceOrientations志群。
以上例子我們發(fā)現(xiàn)只調(diào)用了nav的shouldAutorotate,而棧頂vc的shouldAutorotate方法被忽略了蛔钙,或許我們可以猜測(cè)出锌云,是否能夠旋轉(zhuǎn)取決于vc的父容器即nav。
此時(shí)吁脱,我們手動(dòng)旋轉(zhuǎn)模擬器桑涎,看看打印結(jié)果如何?
2020-08-12 19:32:08.191 rotation[18013:3934444] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 19:32:08.191 rotation[18013:3934444] -[NavVC supportedInterfaceOrientations]
2020-08-12 19:32:08.191 rotation[18013:3934444] -[NavVC shouldAutorotate]
2020-08-12 19:32:08.191 rotation[18013:3934444] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 19:32:08.191 rotation[18013:3934444] -[NavVC supportedInterfaceOrientations]
2020-08-12 19:32:08.199 rotation[18013:3934444] -[NavVC shouldAutorotate]
2020-08-12 19:32:08.199 rotation[18013:3934444] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 19:32:08.200 rotation[18013:3934444] -[NavVC supportedInterfaceOrientations]
2020-08-12 19:32:08.202 rotation[18013:3934444] -[NavVC shouldAutorotate]
2020-08-12 19:32:08.202 rotation[18013:3934444] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 19:32:08.202 rotation[18013:3934444] -[NavVC supportedInterfaceOrientations]
通過(guò)以上打印結(jié)果可以看出,手動(dòng)旋轉(zhuǎn)APP時(shí)兼贡,調(diào)用順序?yàn)锳PPdelegate的application:supportedInterfaceOrientationsForWindow: -> nav的supportedInterfaceOrientations -> nav的shouldAutorotate
即使攻冷,手動(dòng)旋轉(zhuǎn)手機(jī),也沒(méi)有調(diào)用vc的shouldAutorotate方法紧显,甚至連vc的supportedInterfaceOrientations方法都沒(méi)有調(diào)用讲衫,
據(jù)此可以猜測(cè)當(dāng)被旋轉(zhuǎn)的視圖容器為uinavigationcontroller時(shí)缕棵,其vc內(nèi)容是否能夠旋轉(zhuǎn)取決于nav的支持方向孵班;即使我們?cè)O(shè)置vc的方向僅為橫向,最終方向還是取決于nav的支持方向招驴。
為了進(jìn)一步驗(yàn)證猜想篙程,我們點(diǎn)擊push按鈕,跳轉(zhuǎn)到另一個(gè)只允許橫屏的自定義vc(LandscapeRightVC)别厘,打印結(jié)果如下:
2020-08-12 20:08:45.697 rotation[20626:3970125] -[ViewController viewWillDisappear:]
2020-08-12 20:08:45.698 rotation[20626:3970125] -[LandscapeRightVC viewWillAppear:]
2020-08-12 20:08:45.698 rotation[20626:3970125] -[LandscapeRightVC switchToLandscapeRight]
2020-08-12 20:08:45.698 rotation[20626:3970125] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:08:45.698 rotation[20626:3970125] -[NavVC supportedInterfaceOrientations]
2020-08-12 20:08:45.698 rotation[20626:3970125] -[NavVC shouldAutorotate]
2020-08-12 20:08:45.699 rotation[20626:3970125] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:08:45.699 rotation[20626:3970125] -[NavVC supportedInterfaceOrientations]
2020-08-12 20:08:45.705 rotation[20626:3970125] -[NavVC shouldAutorotate]
2020-08-12 20:08:45.706 rotation[20626:3970125] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:08:45.706 rotation[20626:3970125] -[NavVC supportedInterfaceOrientations]
2020-08-12 20:08:45.714 rotation[20626:3970125] -[NavVC shouldAutorotate]
2020-08-12 20:08:45.715 rotation[20626:3970125] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:08:45.726 rotation[20626:3970125] -[NavVC supportedInterfaceOrientations]
通過(guò)打印可以看到只調(diào)用了LandscapeRightVC的旋轉(zhuǎn)方向的方法虱饿,而supportedInterfaceOrientations、shouldAutorotate方法均未調(diào)用触趴,但屏幕方向確實(shí)已經(jīng)橫屏了氮发,之所以能夠橫屏?xí)r因?yàn)榇藭r(shí)nav支持橫屏方向,進(jìn)一步驗(yàn)證了以上觀點(diǎn)冗懦。
我們?cè)賹?dāng)前nav設(shè)置為只支持豎屏爽冕,并且點(diǎn)擊present按鈕,跳轉(zhuǎn)到只支持橫屏的vc看看打印情況:
2020-08-12 20:15:28.471 rotation[20923:3973796] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:15:28.471 rotation[20923:3973796] -[NavVC supportedInterfaceOrientations]
2020-08-12 20:15:28.472 rotation[20923:3973796] -[LandscapeRightVC preferredInterfaceOrientationForPresentation]
2020-08-12 20:15:28.472 rotation[20923:3973796] -[LandscapeRightVC supportedInterfaceOrientations]
2020-08-12 20:15:28.477 rotation[20923:3973796] -[NavVC shouldAutorotate]
2020-08-12 20:15:28.477 rotation[20923:3973796] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:15:28.477 rotation[20923:3973796] -[NavVC supportedInterfaceOrientations]
2020-08-12 20:15:28.477 rotation[20923:3973796] -[LandscapeRightVC shouldAutorotate]
2020-08-12 20:15:28.477 rotation[20923:3973796] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:15:28.478 rotation[20923:3973796] -[LandscapeRightVC supportedInterfaceOrientations]
2020-08-12 20:15:28.481 rotation[20923:3973796] -[ViewController viewWillDisappear:]
2020-08-12 20:15:28.482 rotation[20923:3973796] -[NavVC viewWillDisappear:]
2020-08-12 20:15:28.482 rotation[20923:3973796] -[LandscapeRightVC viewWillAppear:]
2020-08-12 20:15:28.500 rotation[20923:3973796] -[LandscapeRightVC switchToLandscapeRight]
2020-08-12 20:15:29.005 rotation[20923:3973796] -[NavVC shouldAutorotate]
2020-08-12 20:15:29.006 rotation[20923:3973796] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:15:29.006 rotation[20923:3973796] -[NavVC supportedInterfaceOrientations]
2020-08-12 20:15:29.007 rotation[20923:3973796] -[LandscapeRightVC shouldAutorotate]
2020-08-12 20:15:29.008 rotation[20923:3973796] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-12 20:15:29.008 rotation[20923:3973796] -[LandscapeRightVC supportedInterfaceOrientations]
執(zhí)行順序大致是:
[AppDelegate application:supportedInterfaceOrientationsForWindow:] ->
[NavVC supportedInterfaceOrientations] ->
[LandscapeRightVC preferredInterfaceOrientationForPresentation] ->
[LandscapeRightVC supportedInterfaceOrientations] ->
[NavVC shouldAutorotate] ->
[LandscapeRightVC shouldAutorotate] ->
[LandscapeRightVC supportedInterfaceOrientations]
通過(guò)present后確實(shí)已經(jīng)橫屏披蕉,得出結(jié)論颈畸,如果是從vc中present出來(lái)的視圖,即使上一層nav不支持橫屏没讲,最終屏幕旋轉(zhuǎn)方向取決于被present出來(lái)的視圖容器所支持的屏幕方向眯娱。
總結(jié):
屏幕旋轉(zhuǎn)的需求大概有兩種:
1.通過(guò)翻轉(zhuǎn)手機(jī)自動(dòng)旋轉(zhuǎn)內(nèi)容
2.項(xiàng)目只支持豎屏或橫屏模式,個(gè)別頁(yè)面需要強(qiáng)制橫屏或豎屏展示
以上兩種需求其實(shí)都可以通過(guò) 設(shè)置appdelegate -> nav或tabbar 來(lái)支持需要旋轉(zhuǎn)的方向爬凑,唯一區(qū)別在于需求1中無(wú)需強(qiáng)制執(zhí)行代碼來(lái)旋轉(zhuǎn)方向徙缴,屬于被動(dòng)旋轉(zhuǎn);而需求2則要在需要旋轉(zhuǎn)到固定方向的vc中做強(qiáng)制旋轉(zhuǎn)嘁信。
//通過(guò)代碼強(qiáng)制旋轉(zhuǎn)
//為了防止plus及ipad機(jī)型自帶桌面旋轉(zhuǎn)功能時(shí)于样,橫屏失敗迁霎,所以先強(qiáng)制旋轉(zhuǎn)到其他方向,再設(shè)置橫屏才會(huì)有效果
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
那么對(duì)于一個(gè)項(xiàng)目中個(gè)別頁(yè)面需要橫屏?xí)r百宇,我們大致可以進(jìn)行如下寫(xiě)法:
//AppDelegate中
@property(nonatomic, assign, getter=isSupportLandscape) BOOL supportLandscape;
+ (AppDelegate *)sharedInstance {
return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSLog(@"%s",__FUNCTION__);
if (self.isSupportLandscape) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
//自定義的Navigationcontroller中
//雖然nav是控制旋轉(zhuǎn)的主要因素考廉,但為了保持和最上層vc的旋轉(zhuǎn)方向一致,這里統(tǒng)一取topvc的方向設(shè)置
- (BOOL)shouldAutorotate {
NSLog(@"%s",__FUNCTION__);
return [self.viewControllers.lastObject shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSLog(@"%s",__FUNCTION__);
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(@"%s",__FUNCTION__);
return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
//需要橫屏展示的vc中
@interface LandscapeRightVC ()
@property (nonatomic, assign) UIInterfaceOrientationMask orientationMask;
@end
@implementation LandscapeRightVC
- (instancetype)init {
self = [super init];
if (self) {
_orientationMask = UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = NSStringFromClass([self class]);
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[AppDelegate sharedInstance].supportLandscape = YES;
NSLog(@"%s",__FUNCTION__);
[self switchToLandscapeRight];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[AppDelegate sharedInstance].supportLandscape = NO;
NSLog(@"%s",__FUNCTION__);
[self switchToLandscapePortrait];
}
- (BOOL)shouldAutorotate {
NSLog(@"%s",__FUNCTION__);
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(@"%s",__FUNCTION__);
//這里加入[AppDelegate sharedInstance].supportLandscape判斷携御,是為了防止appdelegate中不支持目標(biāo)方向昌粤,而產(chǎn)生Supported orientations has no common orientation with the application的錯(cuò)誤
if ([AppDelegate sharedInstance].supportLandscape) {
if (_orientationMask & UIInterfaceOrientationMaskLandscapeLeft) {
return UIInterfaceOrientationLandscapeLeft;
}
}
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSLog(@"%s",__FUNCTION__);
//這里加入[AppDelegate sharedInstance].supportLandscape判斷,是為了防止appdelegate中不支持目標(biāo)方向啄刹,而產(chǎn)生Supported orientations has no common orientation with the application的錯(cuò)誤
if ([AppDelegate sharedInstance].supportLandscape) {
return _orientationMask | UIInterfaceOrientationMaskLandscapeLeft;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
// 手動(dòng)設(shè)置橫屏
- (void)switchToLandscapeRight {
NSLog(@"%s",__FUNCTION__);
_orientationMask = UIInterfaceOrientationMaskLandscapeLeft;
//為了防止plus及ipad機(jī)型自帶桌面旋轉(zhuǎn)功能時(shí)涮坐,橫屏失敗,所以先強(qiáng)制旋轉(zhuǎn)到其他方向誓军,再設(shè)置橫屏才會(huì)有效果
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
}
// 手動(dòng)設(shè)置豎屏
- (void)switchToLandscapePortrait {
NSLog(@"%s",__FUNCTION__);
_orientationMask = UIInterfaceOrientationMaskPortrait;
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
}
延伸:測(cè)試一下UITabbarController是如何響應(yīng)的
假如將rootviewcontroller設(shè)置為tabbar袱讹,并將tabbar設(shè)置為只支持豎屏,而將其中一個(gè)item設(shè)置為L(zhǎng)andscapeRightVC昵时,打印結(jié)果如下:
2020-08-13 18:19:59.440 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.440 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.443 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.443 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.443 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.444 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.444 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.444 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.453 rotation[84659:4616469] -[LandscapeRightVC viewWillAppear:]
2020-08-13 18:19:59.453 rotation[84659:4616469] -[LandscapeRightVC switchToLandscapeRight]
2020-08-13 18:19:59.453 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.454 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.454 rotation[84659:4616469] -[CusTabViewController shouldAutorotate]
2020-08-13 18:19:59.454 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.454 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.455 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.455 rotation[84659:4616469] -[LandscapeRightVC supportedInterfaceOrientations]
2020-08-13 18:19:59.456 rotation[84659:4616469] -[CusTabViewController shouldAutorotate]
2020-08-13 18:19:59.456 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.457 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.457 rotation[84659:4616469] -[CusTabViewController shouldAutorotate]
2020-08-13 18:19:59.457 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.457 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.458 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.458 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
2020-08-13 18:19:59.458 rotation[84659:4616469] -[AppDelegate application:supportedInterfaceOrientationsForWindow:]
2020-08-13 18:19:59.458 rotation[84659:4616469] -[CusTabViewController supportedInterfaceOrientations]
可以看到tabbar的傳遞順序與nav類似捷雕,并且屬于是視圖容器,假如tabbar不支持橫屏壹甥,那么子控制器無(wú)論如何旋轉(zhuǎn)都不會(huì)有效
順便提一提UIDeviceOrientation(設(shè)備方向)和UIInterfaceOrientation(UI方向)的區(qū)別
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} API_UNAVAILABLE(tvos);
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
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
} API_UNAVAILABLE(tvos);
可以看到橫向的方向命名是相反的救巷,在使用時(shí)需要注意
思考:如果rootviewcontroller是nav,而nav的root是tabbar句柠,tabbar的item又是nav 那么子視圖的旋轉(zhuǎn)又是如何受約束的呢浦译?我猜應(yīng)該是, 取當(dāng)前棧頂容器(如果是push溯职,則取當(dāng)前棧頂容器nav精盅,如果是present,則取當(dāng)前present棧頂中的容器nav或vc谜酒,且present的視圖不受parent視圖的橫豎屏限制)