iOS 16.0强制横屏方法失效

2,004 阅读1分钟

“我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第1篇文章,点击查看活动详情

在iOS 16.0发布后,不出所料的出现适配问题。 今天主要介绍更新iOS16.0后强制横竖屏API失效的处理方案 不多说,直接上代码

    int orientation;
    if (self.m_bFullScreenFlag)
    {
        orientation = UIInterfaceOrientationMaskPortrait;
    }
    else
    {
        orientation = UIInterfaceOrientationMaskLandscapeRight;
    }
    if (@available(iOS 16.0,*)){
        AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
        appdelegate.surrpotOrientation = orientation;
        UINavigationController *nav = [_m_objLiveBarProtocol getViewController].navigationController;
        [nav setNeedsUpdateOfSupportedInterfaceOrientations];
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *ws = (UIWindowScene *)array.firstObject;
        UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
        geometryPreferences.interfaceOrientations = orientation;
        [ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
                NSLog(@"iOS 16 Error: %@",error);
            }];
    }else{
        AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
        [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[self getViewController].view.window];
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        [invocation setArgument:&orientation atIndex:2];
        [invocation invoke];
        [UIViewController attemptRotationToDeviceOrientation];
    }

第二个地方是在AppDelegate中

AppDelegate.h

@property (nonatomic, assign) UIInterfaceOrientationMask surrpotOrientation;
AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(**nullable** UIWindow *)window{
    NSLog(@"doRotateAction---start: surrort %d",**self**.surrpotOrientation);
    if (@available(iOS 16.0,*)){
        return self.surrpotOrientation;
    }else{
        return UIInterfaceOrientationMaskAll;
    }
}

好了,到这里横竖屏的问题基本就处理了,如有问题,欢迎指正。