iOS16适配-屏幕旋转

程序员 2024-10-7 01:17:09 100 0 来自 中国
声明:本文适配以iOS 16 bate 2为基准

配景

iOS 16在UIKIT上有了一些更改,废弃掉了一些修改方式,比如屏幕的横竖屏旋转,这一块之前有许多中处理处罚方法,但是如果之前用的是基于UIDevice的,那在这次更新后就会遇到逼迫旋转屏幕不乐成,且有如下日记提示。
1.png 适配条件

在iOS 16中,我们遇到了页面旋转不乐成的题目,最初的代码采取如下情势
@try {    if ([[UIDevice currentDevice] respondsToSelectorselector(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.png 适配过程

当满足适配条件的时间,我们如今看一下怎样去进行修改。
根据现有提示,我们可以从图1看到内里的提示中,UIWindowScene.requestGeometryUpdate(_为尺度的Swift写法,由此我们可以去追溯至UIWindowScene相应的官方文档,我们可以得到如下提示
3.png 由此我们可以确定,此方法为iOS16中新增的了。
然后根据文档中OC的链接也可以得到如下的方法requestGeometryUpdateWithPreferences:errorHandler:
4.png 但是在这个方法里,有个点肯定要注意,内里须要传入一个UIWindowSceneGeometryPreferences对象,这个也是本次新增的。
但是你在直接对这个对象初始化的时间会发现,这是一个空的抽象类,而且API中会有提示。
此处略坑,根据这个提示,这个对象肯定会有派生类去进行初始化,那这个时间我们点开API,就可以得到如下两个派生类。
7.png 那么点到相应的派生类中,我们终于找到了初始化的方法。
至此,整个新API的探求过程就OK了,然后我们进行一下现实实验。
别的,在实验中发现 application:supportedInterfaceOrientationsForWindow: 署理方法在iOS16中会有调用不到的环境,这里我们之进步行了可用旋转状态的处理处罚。
- (UIInterfaceOrientationMask)applicationUIApplication *)application supportedInterfaceOrientationsForWindowUIWindow *)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);那么剩下的就是,根据我们自身差别的业务场景,进行兼容或者重构即可。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:43, Processed in 0.166755 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表