iOS16屏幕逼迫横屏

源代码 2024-9-13 13:36:36 99 0 来自 中国
适配iOS16横屏操纵
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){                  NSLog(@"调用了Block%@",err);            };            if ([ws respondsToSelector:sel_method]) {                (((void (*)(id, SEL,id,id))[ws methodForSelector:sel_method])(ws, sel_method,geometryPreferences,ErrorBlock));            }        } @catch (NSException *exception) {            NSLog(@"*****************************富丽分割线**************************");            NSLog(@"%@",exception);        } @finally {                    }    } else {        // 逼迫旋转返来        if ([[UIDevice currentDevice] respondsToSelectorselector(setOrientation]) {            SEL selector = NSSelectorFromString(@"setOrientation:");            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];            [invocation setSelector:selector];            [invocation setTarget:[UIDevice currentDevice]];            int val = UIInterfaceOrientationPortrait;            [invocation setArgument:&val atIndex:2];            [invocation invoke];        }    }2022.09.02更新
在调用上面的方法前实行以下方法
/** 控制旋转 */- (void)configSupportedInterfaceOrientationsint)number{    id appDel = [[UIApplication sharedApplication] delegate];    SEL sel_clientstate = NSSelectorFromString(@"setClientstate:");    if ([appDel respondsToSelector:sel_clientstate]) {        (((void (*)(id, SEL, NSUInteger))[appDel methodForSelector:sel_clientstate])(appDel, sel_clientstate, number));    }}此中clientstate这个是一个状态值,用来控制 supportedInterfaceOrientationsFor
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {        switch clientstate {        case 1:            return .allButUpsideDown        case 2:            return .landscape        default:            return .portrait        }    }
您需要登录后才可以回帖 登录 | 立即注册

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

GMT+8, 2024-10-18 22:36, Processed in 0.193012 second(s), 32 queries.© 2003-2025 cbk Team.

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