iOS16屏幕旋转

2022-10-14  本文已影响0人  LWide

iOS16出来一段时间了,个别app出现屏幕不能强制旋转全屏,原因就是iOS不再支持UIDevice 方式的旋转
下面是适配代码,请参考:

首先是在Appdelegate里面添加代码

@property (nonatomic, assign, getter=isLaunchScreen) BOOL launchScreen;    /**< 是否是横屏 */

- (void)setLaunchScreen:(BOOL)launchScreen {

    _launchScreen = launchScreen;
    [self application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:nil];
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (self.isLaunchScreen) {
        return UIInterfaceOrientationMaskLandscapeRight;
    }

    return UIInterfaceOrientationMaskPortrait;
}

然后再需要全屏的界面添加如下代码

- (void)screenChangeWithIsFull:(BOOL)isFull
{
    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (isFull) {
        // 全屏操作
        appdelegate.launchScreen = YES;
    } else {
        // 退出全屏操作
        appdelegate.launchScreen = NO;
    }
    
    if (@available(iOS 16.0, *)) {
        [self setNeedsUpdateOfSupportedInterfaceOrientations];
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *scene = [array firstObject];
        UIInterfaceOrientationMask orientation = isFull ? UIInterfaceOrientationMaskLandscapeRight : UIInterfaceOrientationMaskPortrait;
        UIWindowSceneGeometryPreferencesIOS *geometryPreferencesIOS = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientation];
        [scene requestGeometryUpdateWithPreferences:geometryPreferencesIOS errorHandler:^(NSError * _Nonnull error) {
            NSLog(@"强制%@错误:%@", isFull ? @"横屏" : @"竖屏", error);
        }];
    } else {
        UIInterfaceOrientation interfaceOrientation =  isFull ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait;
        NSNumber *orientationTarget = [NSNumber numberWithInteger:interfaceOrientation];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    }
    
}

感谢你的查看,喜欢的麻烦点个赞.

上一篇下一篇

猜你喜欢

热点阅读