美文网首页
iOS16适配:APP切换横竖屏问题

iOS16适配:APP切换横竖屏问题

作者: 喔牛慢慢爬 | 来源:发表于2022-09-15 14:28 被阅读0次

iOS16之前切换横竖屏使用的是UIDevicesetValue:forKey:方法进行切换。但在iOS16后不再支持使用setValue方法设置设备的方向,建议替换为UIWindowScenerequestGeometryUpdate方法。

iOS16之前切换屏幕方法:

///竖屏
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
///横屏
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];

iOS16之后切换屏幕方法:

///竖屏
if (@available(iOS 16.0, *)) {
    [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
    NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
    UIWindowScene *ws = (UIWindowScene *)array[0];
    UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
    geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
    [ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
         //业务代码
    }];
} else {
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}
//横屏
if (@available(iOS 16.0, *)) {
    [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
    NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
    UIWindowScene *ws = (UIWindowScene *)array[0];
    UIWindowSceneGeometryPreferencesIOS *geometryPreferences =     [[UIWindowSceneGeometryPreferencesIOS alloc] init];
    geometryPreferences.interfaceOrientations =     UIInterfaceOrientationMaskLandscapeLeft;
    [ws requestGeometryUpdateWithPreferences:geometryPreferences     errorHandler:^(NSError * _Nonnull error) {
         //业务代码
    }];
} else {
    [[UIDevice currentDevice] setValue:[NSNumber   numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
}

注意:[self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];这段代码,需要写在在转屏的页面合适位置,否则转屏不起作用,导致监听转屏的方法viewWillTransitionToSize: viewWillTransitionToSize未被回调(自动转屏时也不会回调)。

相关文章

  • iOS16适配:APP切换横竖屏问题

    iOS16之前切换横竖屏使用的是UIDevice的setValue:forKey:方法进行切换。但在iOS16后不...

  • iOS 16强制切换横竖屏失效解决

    ios16切换横竖屏代码 注意:1.ios16 开始 UIDeviceOrientationDidChangeNo...

  • iOS:关于横竖屏切换的知识点

    iOS16横竖屏的切换有了新的方式,正好赶上新的项目要求,所以重新整理了一下项目中的横竖屏切换问题。项目要求: i...

  • iOS16 横竖屏切换适配

    项目中针对某一个 View 需要进行横屏,在 iOS16 之前的方式大部分都是采取设置设备的方向来实现的,但是在 ...

  • android 横竖屏切换经验总结

    横竖屏切换已经不是什么难的了,因为要适配手机横竖屏,所以特别研究了一下,再次系统的讲讲干货。主要是横竖屏切换,不重...

  • 关于iOS横竖屏适配

    关于iOS横竖屏适配 关于iOS横竖屏适配

  • 横竖屏旋转切换

    一、代码控制横竖屏切换 二、App的横竖屏切换和许多因素有关 info.plist appDelegate Tab...

  • 横竖屏限制

    (转)当手机没有关闭横竖屏切换功能时,系统一旦触发横竖屏切换,缺省状态下,当前活动的App的界面就会进行横竖屏切换...

  • 3.2.3 Activity的使用技巧汇总

    一、横竖屏切换与状态保存的问题 前面的文章说到了App横竖屏切换的时候会销毁当前的Activity然后重新创建一个...

  • JS 与 IOS 交互-横竖屏切换

    IOS 设备横竖屏情况 一般情形 所有界面都支持横竖屏切换如果App的所有切面都要支持横竖屏的切换,那只需要勾选【...

网友评论

      本文标题:iOS16适配:APP切换横竖屏问题

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