iOS 關(guān)于橫豎屏問(wèn)題

關(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è)面布局如下:


download.png

分別自定義了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]


image.png

通過(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視圖的橫豎屏限制)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末叹俏,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子甚带,更是在濱河造成了極大的恐慌她肯,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,816評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鹰贵,死亡現(xiàn)場(chǎng)離奇詭異晴氨,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)碉输,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門籽前,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事枝哄∫蘩妫” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,300評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵挠锥,是天一觀的道長(zhǎng)众羡。 經(jīng)常有香客問(wèn)我,道長(zhǎng)蓖租,這世上最難降的妖魔是什么粱侣? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,780評(píng)論 1 285
  • 正文 為了忘掉前任,我火速辦了婚禮蓖宦,結(jié)果婚禮上齐婴,老公的妹妹穿的比我還像新娘。我一直安慰自己稠茂,他們只是感情好柠偶,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,890評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著睬关,像睡著了一般诱担。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上共螺,一...
    開(kāi)封第一講書(shū)人閱讀 50,084評(píng)論 1 291
  • 那天该肴,我揣著相機(jī)與錄音情竹,去河邊找鬼藐不。 笑死,一個(gè)胖子當(dāng)著我的面吹牛秦效,可吹牛的內(nèi)容都是我干的雏蛮。 我是一名探鬼主播,決...
    沈念sama閱讀 39,151評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼阱州,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼挑秉!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起苔货,我...
    開(kāi)封第一講書(shū)人閱讀 37,912評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤犀概,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后夜惭,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體姻灶,經(jīng)...
    沈念sama閱讀 44,355評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,666評(píng)論 2 327
  • 正文 我和宋清朗相戀三年诈茧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了产喉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,809評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖曾沈,靈堂內(nèi)的尸體忽然破棺而出这嚣,到底是詐尸還是另有隱情,我是刑警寧澤塞俱,帶...
    沈念sama閱讀 34,504評(píng)論 4 334
  • 正文 年R本政府宣布姐帚,位于F島的核電站,受9級(jí)特大地震影響障涯,放射性物質(zhì)發(fā)生泄漏卧土。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,150評(píng)論 3 317
  • 文/蒙蒙 一像樊、第九天 我趴在偏房一處隱蔽的房頂上張望尤莺。 院中可真熱鬧,春花似錦生棍、人聲如沸颤霎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,882評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)友酱。三九已至,卻和暖如春柔纵,著一層夾襖步出監(jiān)牢的瞬間缔杉,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,121評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工搁料, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留或详,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,628評(píng)論 2 362
  • 正文 我出身青樓郭计,卻偏偏與公主長(zhǎng)得像霸琴,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子昭伸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,724評(píng)論 2 351