iOS16适配-屏幕旋转

作者: 不正常人类研究中心驴主任 | 来源:发表于2022-06-30 15:45 被阅读0次

    声明:本文适配以iOS 16 bate 2为基准

    背景

    iOS 16在UIKIT上有了一些更改,废弃掉了一些修改方式,比如屏幕的横竖屏旋转,这一块之前有很多中处理方法,但是如果之前用的是基于UIDevice的,那在这次更新后就会遇到强制旋转屏幕不成功,且有如下日志提示。

    图1

    适配前提

    在iOS 16中,我们遇到了页面旋转不成功的问题,最初的代码采用如下形式

    @try {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            UIDeviceOrientation val = UIDeviceOrientationLandscapeLeft;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    } @catch (NSException *exception) {
        
    } @finally {
        
    }
    

    但是在执行的时候,就会出现屏幕旋转失败,日志提示图1的内容。

    根据日志提示,我们可以看到,UIDevice.orientation这个形式已经不再被支持,此处其实核心点就在于,我们之前进行的setOrientation其实是用KVC的形式去修改UIDevice里的readonly对象orientation。

    但是在iOS 16里不知道为什么这个方法被禁用了,但是在API里又没有发现提示。

    图2

    适配过程

    当满足适配前提的时候,我们现在看一下如何去进行修改。

    根据现有提示,我们可以从图1看到里面的提示中,UIWindowScene.requestGeometryUpdate(_:)为标准的Swift写法,由此我们可以去追溯至UIWindowScene相应的官方文档,我们可以得到如下提示

    图3

    由此我们可以确定,此方法为iOS16中新增的了。

    然后根据文档中OC的链接也可以得到如下的方法requestGeometryUpdateWithPreferences:errorHandler:

    图4

    但是在这个方法里,有个点一定要注意,里面需要传入一个UIWindowSceneGeometryPreferences对象,这个也是本次新增的。

    图5

    但是你在直接对这个对象初始化的时候会发现,这是一个空的抽象类,并且API中会有提示。

    图6

    此处略坑,根据这个提示,这个对象一定会有派生类去进行初始化,那这个时候我们点开API,就可以得到如下两个派生类。

    图7

    那么点到相应的派生类中,我们终于找到了初始化的方法。

    图8

    至此,整个新API的寻找过程就OK了,然后我们进行一下实际尝试。

    另外,在尝试中发现 application:supportedInterfaceOrientationsForWindow: 代理方法在iOS16中会有调用不到的情况,这里我们之前进行了可用旋转状态的处理。

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

    综合以上情况,手动调用setNeedsUpdateOfSupportedInterfaceOrientations触发一下application:supportedInterfaceOrientationsForWindow,我们可以用如下形式进行解决

    if (@available(iOS 16.0, *)) {
        [vc.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
        
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *ws = (UIWindowScene *)array[0];
        UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
        geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeLeft;
        [ws requestGeometryUpdateWithPreferences:geometryPreferences
            errorHandler:^(NSError * _Nonnull error) {
            //业务代码
        }];
    }
    

    如果暂时不能用Xcode14,那么可以用如下形式解决,原理和上面是一样的

    if (@available(iOS 16.0, *)) {
        @try {
            NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
            UIWindowScene *ws = (UIWindowScene *)array[0];
            Class GeometryPreferences = NSClassFromString(@"UIWindowSceneGeometryPreferencesIOS");
            id geometryPreferences = [[GeometryPreferences alloc]init];
            [geometryPreferences setValue:@(UIInterfaceOrientationMaskLandscapeRight) forKey:@"interfaceOrientations"];
            SEL sel_method = NSSelectorFromString(@"requestGeometryUpdateWithPreferences:errorHandler:");
            void (^ErrorBlock)(NSError *err) = ^(NSError *err){
                //业务代码
            };
            if ([ws respondsToSelector:sel_method]) {
                (((void (*)(id, SEL,id,id))[ws methodForSelector:sel_method])(ws, sel_method,geometryPreferences,ErrorBlock));
            }
        } @catch (NSException *exception) {
            //异常处理
        } @finally {
            //异常处理
        }
    }
    

    这样就可以旋转屏幕了。

    具体的旋转方向,我们可以修改UIWindowSceneGeometryPreferencesIOS中的interfaceOrientations属性,这个属性为页面方向UIInterfaceOrientationMask枚举。

    typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
        UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
        UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
        UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
        UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
        UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
        UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
        UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    } API_UNAVAILABLE(tvos);
    

    那么剩下的就是,根据我们自身不同的业务场景,进行兼容或者重构即可。

    相关文章

      网友评论

        本文标题:iOS16适配-屏幕旋转

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