iOS 关于横竖屏问题

关于横竖屏相关方法的响应都是迷之存在,很难琢磨,搞的一头雾水。最近项目中正好遇到某个控制器需要横屏展示,查阅WWDC资料未发现关于orientation或者rotation相关资料。
只能通过实验与猜测大致了解其生命周期了。
首先,当前屏幕是否支持横竖屏旋转取决于当前window的支持方向。
设置window横竖屏的地方有两处,
第一处,通过工程->General->DeviceOrientation来勾选支持的方向
第二处,通过重写Appdelegate中的方法来设置当前window支持的方向

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

那么问题来了,既然有两处都可以设置,那么最终取值以哪一个为准呢?
通过测试发现,如果两处都设置了,最终以第二种设置为(主要)判断依据;如果第二处未设置,则以第一处为准。
只有当前window支持某个方向的旋转,才能对控制器(如UIviewcontroller)进行相应方向的旋转并达到想要的效果(内容跟着转动)。
设置了可旋转的方向之后,就要对个别控制器做更细致的限制了,比如想让A控制只能竖屏,或者让B控制器只能横屏,就需要用到VC的扩展方法(系统自带的)

//是否允许旋转
- (BOOL)shouldAutorotate {
    NSLog(@"%s",__FUNCTION__);
    return YES;
}
//present时可支持的旋转方向(present时在supportedInterfaceOrientations前执行)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    NSLog(@"%s",__FUNCTION__);
    if (_orientationMask & UIInterfaceOrientationMaskLandscapeLeft) {
        return UIInterfaceOrientationLandscapeLeft;
    }
    return UIInterfaceOrientationPortrait;
}
//支持的旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    NSLog(@"%s",__FUNCTION__);
    if ([AppDelegate sharedInstance].supportLandscape) {
        return _orientationMask | UIInterfaceOrientationMaskLandscapeLeft;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

旋转事件的传递顺序研究

通过创建一个新项目,页面布局如下:


download.png

分别自定义了UINavigationcontroller和UIviewcontroller,并重写了vc旋转相关的三个扩展方法,将UINavigationcontroller设置为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查看旋转方法调用情况如下:

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]

通过以上打印结果可以看出,APP启动时,调用顺序为APPdelegate的application:supportedInterfaceOrientationsForWindow: -> nav的supportedInterfaceOrientations -> nav的shouldAutorotate -> vc的supportedInterfaceOrientations

通过以上结果论证得出结论:APP在监听到旋转时,会首先通知window 并执行delegate中的supportedInterfaceOrientationsForWindow方法,然后通知rootviewcontroller并执行supportedInterfaceOrientations方法,最后通知栈顶vc并执行supportedInterfaceOrientations。
以上例子我们发现只调用了nav的shouldAutorotate,而栈顶vc的shouldAutorotate方法被忽略了,或许我们可以猜测出,是否能够旋转取决于vc的父容器即nav。
此时,我们手动旋转模拟器,看看打印结果如何?

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]

通过以上打印结果可以看出,手动旋转APP时,调用顺序为APPdelegate的application:supportedInterfaceOrientationsForWindow: -> nav的supportedInterfaceOrientations -> nav的shouldAutorotate
即使,手动旋转手机,也没有调用vc的shouldAutorotate方法,甚至连vc的supportedInterfaceOrientations方法都没有调用,

据此可以猜测当被旋转的视图容器为uinavigationcontroller时,其vc内容是否能够旋转取决于nav的支持方向;即使我们设置vc的方向仅为横向,最终方向还是取决于nav的支持方向。

为了进一步验证猜想,我们点击push按钮,跳转到另一个只允许横屏的自定义vc(LandscapeRightVC),打印结果如下:

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]

通过打印可以看到只调用了LandscapeRightVC的旋转方向的方法,而supportedInterfaceOrientations、shouldAutorotate方法均未调用,但屏幕方向确实已经横屏了,之所以能够横屏时因为此时nav支持横屏方向,进一步验证了以上观点。

我们再将当前nav设置为只支持竖屏,并且点击present按钮,跳转到只支持横屏的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]

执行顺序大致是:
[AppDelegate application:supportedInterfaceOrientationsForWindow:] ->
[NavVC supportedInterfaceOrientations] ->
[LandscapeRightVC preferredInterfaceOrientationForPresentation] ->
[LandscapeRightVC supportedInterfaceOrientations] ->
[NavVC shouldAutorotate] ->
[LandscapeRightVC shouldAutorotate] ->
[LandscapeRightVC supportedInterfaceOrientations]


image.png

通过present后确实已经横屏,得出结论,如果是从vc中present出来的视图,即使上一层nav不支持横屏,最终屏幕旋转方向取决于被present出来的视图容器所支持的屏幕方向。

总结:

屏幕旋转的需求大概有两种:

1.通过翻转手机自动旋转内容

2.项目只支持竖屏或横屏模式,个别页面需要强制横屏或竖屏展示

以上两种需求其实都可以通过 设置appdelegate -> nav或tabbar 来支持需要旋转的方向,唯一区别在于需求1中无需强制执行代码来旋转方向,属于被动旋转;而需求2则要在需要旋转到固定方向的vc中做强制旋转。

//通过代码强制旋转
//为了防止plus及ipad机型自带桌面旋转功能时,横屏失败,所以先强制旋转到其他方向,再设置横屏才会有效果
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];  
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];

那么对于一个项目中个别页面需要横屏时,我们大致可以进行如下写法:

//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是控制旋转的主要因素,但为了保持和最上层vc的旋转方向一致,这里统一取topvc的方向设置
- (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中不支持目标方向,而产生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中不支持目标方向,而产生Supported orientations has no common orientation with the application的错误
    if ([AppDelegate sharedInstance].supportLandscape) {
        return _orientationMask | UIInterfaceOrientationMaskLandscapeLeft;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

// 手动设置横屏
- (void)switchToLandscapeRight {
    NSLog(@"%s",__FUNCTION__);
    _orientationMask = UIInterfaceOrientationMaskLandscapeLeft;
    //为了防止plus及ipad机型自带桌面旋转功能时,横屏失败,所以先强制旋转到其他方向,再设置横屏才会有效果
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
}

// 手动设置竖屏
- (void)switchToLandscapePortrait {
    NSLog(@"%s",__FUNCTION__);
    _orientationMask = UIInterfaceOrientationMaskPortrait;
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationFaceDown) forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
}

延伸:测试一下UITabbarController是如何响应的

假如将rootviewcontroller设置为tabbar,并将tabbar设置为只支持竖屏,而将其中一个item设置为LandscapeRightVC,打印结果如下:

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不支持横屏,那么子控制器无论如何旋转都不会有效

顺便提一提UIDeviceOrientation(设备方向)和UIInterfaceOrientation(UI方向)的区别


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 那么子视图的旋转又是如何受约束的呢?我猜应该是, 取当前栈顶容器(如果是push,则取当前栈顶容器nav,如果是present,则取当前present栈顶中的容器nav或vc,且present的视图不受parent视图的横竖屏限制)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335