美文网首页
iOS 关于横竖屏问题

iOS 关于横竖屏问题

作者: GoldenChan | 来源:发表于2020-08-13 11:33 被阅读0次

    关于横竖屏相关方法的响应都是迷之存在,很难琢磨,搞的一头雾水。最近项目中正好遇到某个控制器需要横屏展示,查阅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视图的横竖屏限制)

    相关文章

      网友评论

          本文标题:iOS 关于横竖屏问题

          本文链接:https://www.haomeiwen.com/subject/fhoydktx.html