關(guān)于橫豎屏相關(guān)方法的響應(yīng)都是迷之存在,很難琢磨,搞的一頭霧水。最近項目中正好遇到某個控制器需要橫屏展示,查閱WWDC資料未發(fā)現(xiàn)關(guān)于orientation或者rotation相關(guān)資料。
只能通過實驗與猜測大致了解其生命周期了。
首先,當前屏幕是否支持橫豎屏旋轉(zhuǎn)取決于當前window的支持方向。
設(shè)置window橫豎屏的地方有兩處,
第一處,通過工程->General->DeviceOrientation來勾選支持的方向
第二處,通過重寫Appdelegate中的方法來設(shè)置當前window支持的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
那么問題來了,既然有兩處都可以設(shè)置,那么最終取值以哪一個為準呢?
通過測試發(fā)現(xiàn),如果兩處都設(shè)置了,最終以第二種設(shè)置為(主要)判斷依據(jù);如果第二處未設(shè)置,則以第一處為準。
只有當前window支持某個方向的旋轉(zhuǎn),才能對控制器(如UIviewcontroller)進行相應(yīng)方向的旋轉(zhuǎn)并達到想要的效果(內(nèi)容跟著轉(zhuǎn)動)。
設(shè)置了可旋轉(zhuǎn)的方向之后,就要對個別控制器做更細致的限制了,比如想讓A控制只能豎屏,或者讓B控制器只能橫屏,就需要用到VC的擴展方法(系統(tǒng)自帶的)
//是否允許旋轉(zhuǎn)
- (BOOL)shouldAutorotate {
NSLog(@"%s",__FUNCTION__);
return YES;
}
//present時可支持的旋轉(zhuǎn)方向(present時在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)事件的傳遞順序研究
通過創(chuàng)建一個新項目,頁面布局如下:

分別自定義了UINavigationcontroller和UIviewcontroller,并重寫了vc旋轉(zhuǎn)相關(guān)的三個擴展方法,將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;
}
直接啟動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]
通過以上打印結(jié)果可以看出,APP啟動時,調(diào)用順序為APPdelegate的application:supportedInterfaceOrientationsForWindow: -> nav的supportedInterfaceOrientations -> nav的shouldAutorotate -> vc的supportedInterfaceOrientations
通過以上結(jié)果論證得出結(jié)論:APP在監(jiān)聽到旋轉(zhuǎn)時,會首先通知window 并執(zhí)行delegate中的supportedInterfaceOrientationsForWindow方法,然后通知rootviewcontroller并執(zhí)行supportedInterfaceOrientations方法,最后通知棧頂vc并執(zhí)行supportedInterfaceOrientations。
以上例子我們發(fā)現(xiàn)只調(diào)用了nav的shouldAutorotate,而棧頂vc的shouldAutorotate方法被忽略了,或許我們可以猜測出,是否能夠旋轉(zhuǎn)取決于vc的父容器即nav。
此時,我們手動旋轉(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]
通過以上打印結(jié)果可以看出,手動旋轉(zhuǎn)APP時,調(diào)用順序為APPdelegate的application:supportedInterfaceOrientationsForWindow: -> nav的supportedInterfaceOrientations -> nav的shouldAutorotate
即使,手動旋轉(zhuǎn)手機,也沒有調(diào)用vc的shouldAutorotate方法,甚至連vc的supportedInterfaceOrientations方法都沒有調(diào)用,
據(jù)此可以猜測當被旋轉(zhuǎn)的視圖容器為uinavigationcontroller時,其vc內(nèi)容是否能夠旋轉(zhuǎn)取決于nav的支持方向;即使我們設(shè)置vc的方向僅為橫向,最終方向還是取決于nav的支持方向。
為了進一步驗證猜想,我們點擊push按鈕,跳轉(zhuǎn)到另一個只允許橫屏的自定義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]
通過打印可以看到只調(diào)用了LandscapeRightVC的旋轉(zhuǎn)方向的方法,而supportedInterfaceOrientations、shouldAutorotate方法均未調(diào)用,但屏幕方向確實已經(jīng)橫屏了,之所以能夠橫屏?xí)r因為此時nav支持橫屏方向,進一步驗證了以上觀點。
我們再將當前nav設(shè)置為只支持豎屏,并且點擊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]

通過present后確實已經(jīng)橫屏,得出結(jié)論,如果是從vc中present出來的視圖,即使上一層nav不支持橫屏,最終屏幕旋轉(zhuǎn)方向取決于被present出來的視圖容器所支持的屏幕方向。
總結(jié):
屏幕旋轉(zhuǎn)的需求大概有兩種:
1.通過翻轉(zhuǎn)手機自動旋轉(zhuǎn)內(nèi)容
2.項目只支持豎屏或橫屏模式,個別頁面需要強制橫屏或豎屏展示
以上兩種需求其實都可以通過 設(shè)置appdelegate -> nav或tabbar 來支持需要旋轉(zhuǎn)的方向,唯一區(qū)別在于需求1中無需強制執(zhí)行代碼來旋轉(zhuǎn)方向,屬于被動旋轉(zhuǎn);而需求2則要在需要旋轉(zhuǎn)到固定方向的vc中做強制旋轉(zhuǎn)。
//通過代碼強制旋轉(zhuǎn)
//為了防止plus及ipad機型自帶桌面旋轉(zhuǎn)功能時,橫屏失敗,所以先強制旋轉(zhuǎn)到其他方向,再設(shè)置橫屏才會有效果
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
那么對于一個項目中個別頁面需要橫屏?xí)r,我們大致可以進行如下寫法:
//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中不支持目標方向,而產(chǎn)生Supported orientations has no common orientation with the application的錯誤
if ([AppDelegate sharedInstance].supportLandscape) {
if (_orientationMask & UIInterfaceOrientationMaskLandscapeLeft) {
return UIInterfaceOrientationLandscapeLeft;
}
}
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
NSLog(@"%s",__FUNCTION__);
//這里加入[AppDelegate sharedInstance].supportLandscape判斷,是為了防止appdelegate中不支持目標方向,而產(chǎn)生Supported orientations has no common orientation with the application的錯誤
if ([AppDelegate sharedInstance].supportLandscape) {
return _orientationMask | UIInterfaceOrientationMaskLandscapeLeft;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
// 手動設(shè)置橫屏
- (void)switchToLandscapeRight {
NSLog(@"%s",__FUNCTION__);
_orientationMask = UIInterfaceOrientationMaskLandscapeLeft;
//為了防止plus及ipad機型自帶桌面旋轉(zhuǎn)功能時,橫屏失敗,所以先強制旋轉(zhuǎn)到其他方向,再設(shè)置橫屏才會有效果
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
}
// 手動設(shè)置豎屏
- (void)switchToLandscapePortrait {
NSLog(@"%s",__FUNCTION__);
_orientationMask = UIInterfaceOrientationMaskPortrait;
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
}
延伸:測試一下UITabbarController是如何響應(yīng)的
假如將rootviewcontroller設(shè)置為tabbar,并將tabbar設(shè)置為只支持豎屏,而將其中一個item設(shè)置為LandscapeRightVC,打印結(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不支持橫屏,那么子控制器無論如何旋轉(zhuǎn)都不會有效
順便提一提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);
可以看到橫向的方向命名是相反的,在使用時需要注意
思考:如果rootviewcontroller是nav,而nav的root是tabbar,tabbar的item又是nav 那么子視圖的旋轉(zhuǎn)又是如何受約束的呢?我猜應(yīng)該是, 取當前棧頂容器(如果是push,則取當前棧頂容器nav,如果是present,則取當前present棧頂中的容器nav或vc,且present的視圖不受parent視圖的橫豎屏限制)