美文网首页
iOS16屏幕旋转

iOS16屏幕旋转

作者: LWide | 来源:发表于2022-10-14 15:58 被阅读0次

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"];
    }
    
}

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

相关文章

  • iOS16 屏幕旋转

    iOS16 强行让屏幕旋转 UIDevice.current.setValue(UIInterfaceOrient...

  • iOS16屏幕旋转

    iOS16出来一段时间了,个别app出现屏幕不能强制旋转全屏,原因就是iOS不再支持UIDevice 方式的旋转下...

  • iOS16适配-屏幕旋转

    声明:本文适配以iOS 16 bate 2为基准 背景 iOS 16在UIKIT上有了一些更改,废弃掉了一些修改方...

  • iOS16适配 屏幕旋转横屏

    背景 iOS16之前转屏方法, 16之后无效, 导致界面错乱.虽然Xcode14/iOS16提供了新的api但还是...

  • iOS 屏幕旋转

    屏幕旋转 认知 期望达到的目的 如何让App支持屏幕旋转 如何让App屏幕旋转 如何保证屏幕旋转后布局不会乱 总结...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • 屏幕旋转

    UIDevice.current.setValue(UIInterfaceOrientation.landscap...

  • 屏幕旋转

    import "AppDelegate.h" import "ViewController.h" @interfa...

  • 屏幕旋转

    在做工程的时候碰到了屏幕旋转的问题,如今已经解决.为大家分享一下 屏幕旋转机制流程 (1)加速计检测到方向变化,发...

  • 屏幕旋转

    每个视图控制器都控制着自己的旋转方向,如果需要新的旋转权限需要模态出新的视图控制器(如navigation tab...

网友评论

      本文标题:iOS16屏幕旋转

      本文链接:https://www.haomeiwen.com/subject/sdlczrtx.html