分享

iOS16屏幕强制横屏

 厚德丶载福 2023-09-11

适配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] respondsToSelector:@selector(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)configSupportedInterfaceOrientations:(int)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 } }
最后编辑于:2022-09-02 15:34

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多