美文网首页
iOS 设置全局横屏,特定页面允许横屏

iOS 设置全局横屏,特定页面允许横屏

作者: 蓝白自由 | 来源:发表于2023-06-11 11:04 被阅读0次
如果在 info.plist 或 TARGETS --> General --> Deployment Info 里配置了支持横屏的方向
且 手机有设置允许横屏
此时 App 页面是跟随手机设备的状态而横竖屏的

场景一、如果我手机锁定(如下图)
则 此时 App 页面 只可以是竖屏的状态

进阶版:如果已在 AppDelegate.m 里设置了禁止全局横屏,特定页面想要实现允许横屏,如何操作?
- (UIInterfaceOrientationMask)application:(UIApplication *)application
  supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait; // 默认全局不支持横屏
}
是需要修改代码逻辑的,具体操作如下:

1、在 AppDelegate.h 添加以下属性:

// 是否 允许横屏的 标记 
@property (nonatomic,assign)BOOL allowRotation;

2、在 Appdelegate.m 添加如下代码:

- (UIInterfaceOrientationMask)application:(UIApplication *)application
  supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) { // 如果设置了 allowRotation 属性,支持全屏
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait; // 默认全局不支持横屏
}

3、在需要支持横屏的界面改变 allowRotation 属性

// 进入全屏
- (void)enterViewAction {
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
    
    if (@available(iOS 16.0, *)) {

        [self setNeedsUpdateOfSupportedInterfaceOrientations];
        [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *scene = (UIWindowScene *)array[0];
        UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
        [scene requestGeometryUpdateWithPreferences:geometryPreferences
                                       errorHandler:^(NSError * _Nonnull error) {
            NSLog(@"\n== Error 了: %@", error);
        }];

    } else {

    }
 
}

// 退出全屏
- (void)quitViewAction {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    if (@available(iOS 16.0, *)) {
        // 同样使用 requestGeometryUpdate 方法
    } 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];
        }
        
    }
    
}

4、在 viewWillAppear 和 viewWillDisappear 分别调用以上方法,在该控制器下即可顺利支持全屏 。


下面方法可能在 iOS 16.0 + 上无效,还需要进一步测试。

// 进入全屏
- (void)enterViewAction {
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

// 退出全屏
- (void)quitViewAction {
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

相关文章

网友评论

      本文标题:iOS 设置全局横屏,特定页面允许横屏

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