iOS16适配 屏幕旋转横屏

开发者 2024-9-12 00:22:47 33 0 来自 中国
配景

iOS16之前转屏方法setOrientation:, 16之后使用无效.
固然Xcode14/iOS16提供了新的api但照旧beta版, 不能直接打包上线, 以是要在旧版适配新版本.
怎么适配 实验了很多方法, 好比横屏时直接present一个横屏VC, 但耗时耗力, 怎么花最小代价适配iOS16, 看下面...
未升级Xcode14提前调试iOS16

下载iOS16体系支持包
放到这个件夹下, 重启Xcode就可以了
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
iOS16新增转屏api

    UINavigationController *nav = [TYToolsUtil topViewController].navigationController;    [nav setNeedsUpdateOfSupportedInterfaceOrientations];        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];    UIWindowScene *ws = (UIWindowScene *)array.firstObject;    UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];    geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;    [ws requestGeometryUpdateWithPreferences:geometryPreferences        errorHandler:^(NSError * _Nonnull error) {        NSLog(@"iOS 16 转屏Error: %@",error);    }];但在升级xcode14之前不能直接调用到, 可以通过获取方法IMP调用
以是可以先这么适配, 上代码:
+ (void)setDevOriUIDeviceOrientation)ori {    @try {        // ios16使用新的api        if (@available(iOS 16.0, *)) {            UIInterfaceOrientationMask oriMask = UIInterfaceOrientationMaskPortrait;            if (ori == UIDeviceOrientationPortrait) {                oriMask = UIInterfaceOrientationMaskPortrait;            } else if (ori == UIDeviceOrientationLandscapeLeft) {                oriMask = UIInterfaceOrientationMaskLandscapeRight;            } else if (ori == UIDeviceOrientationLandscapeRight) {                oriMask = UIInterfaceOrientationMaskLandscapeLeft;            } else {                return;            }            // 防止appDelegate supportedInterfaceOrientationsForWindow方法不调用            UINavigationController *nav = [TYToolsUtil topViewController].navigationController;            SEL selUpdateSupportedMethod = NSSelectorFromString(@"setNeedsUpdateOfSupportedInterfaceOrientations");            if ([nav respondsToSelector:selUpdateSupportedMethod]) {                (((void (*)(id, SEL))[nav methodForSelector:selUpdateSupportedMethod])(nav, selUpdateSupportedMethod));            }                        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];            UIWindowScene *ws = (UIWindowScene *)array.firstObject;            Class GeometryPreferences = NSClassFromString(@"UIWindowSceneGeometryPreferencesIOS");            id geometryPreferences = [[GeometryPreferences alloc] init];            [geometryPreferences setValue(oriMask) forKey"interfaceOrientations"];            SEL selGeometryUpdateMethod = NSSelectorFromString(@"requestGeometryUpdateWithPreferences:errorHandler:");            void (^ErrorBlock)(NSError *error) = ^(NSError *error){                  NSLog(@"iOS 16 转屏Error: %@",error);            };            if ([ws respondsToSelector:selGeometryUpdateMethod]) {                (((void (*)(id, SEL,id,id))[ws methodForSelector:selGeometryUpdateMethod])(ws, selGeometryUpdateMethod,geometryPreferences,ErrorBlock));            }        } else {                        if ([[UIDevice currentDevice] respondsToSelectorselector(setOrientation]) {                SEL selector = NSSelectorFromString(@"setOrientation:");                if ([UIDevice currentDevice].orientation == ori) {                    NSInvocation *invocationUnknow = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];                    [invocationUnknow setSelector:selector];                    [invocationUnknow setTarget:[UIDevice currentDevice]];                    UIDeviceOrientation unKnowVal = UIDeviceOrientationUnknown;                    [invocationUnknow setArgument:&unKnowVal atIndex:2];                    [invocationUnknow invoke];                }                                NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];                [invocation setSelector:selector];                [invocation setTarget:[UIDevice currentDevice]];                UIDeviceOrientation val = ori;                [invocation setArgument:&val atIndex:2];                [invocation invoke];            }        }            } @catch (NSException *exception) {            } @finally {            }}留意: 使用iOS16方法, 调转屏方法后立即处理处罚UI, 大概造成界面繁芜.
接待大佬批评交换??
您需要登录后才可以回帖 登录 | 立即注册

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

GMT+8, 2024-11-22 00:19, Processed in 0.175866 second(s), 32 queries.© 2003-2025 cbk Team.

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