美文网首页iOS
iOS ~ 在全局竖屏的情况下,设置某一个页面强制横屏

iOS ~ 在全局竖屏的情况下,设置某一个页面强制横屏

作者: 阳光下的叶子呵 | 来源:发表于2022-05-18 19:07 被阅读0次

(1)需要跟随手机系统自动旋转才需要添加方法-shouldAutorotate:
(2)强制横屏:一般只需要设置:supportedInterfaceOrientations,这个只能present页面才可以实现,push页面无效。
相关链接:①:iOS强制横屏或强制竖屏
②:iOS横屏的深入研究

第一种:适用于:push和present 的页面

/// AppDelegate.h

/** 屏幕方向:
 1:UIInterfaceOrientationMaskPortrait
 2:UIInterfaceOrientationMaskLandscapeLeft
 3:UIInterfaceOrientationMaskLandscapeRight
 4:UIInterfaceOrientationMaskPortraitUpsideDown
 */
@property (nonatomic, assign) NSInteger screenOrientation_rotation_Style;
/// AppDelegate.m

// 某些页面,可以横屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if (_screenOrientation_rotation_Style == 3) {// 如果是3 就让屏幕强制横屏 
        return UIInterfaceOrientationMaskLandscapeRight; // UIInterfaceOrientationMaskLandscapeLeft

    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

在强制横屏的VC中,实现:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"全屏 展示计分";
    
    // 再设置 竖屏
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.screenOrientation_rotation_Style = 3;
    [self switchNewOrientation:UIInterfaceOrientationLandscapeRight];
    
    // 先隐藏状态栏、导航栏,再自定义返回按钮:
    self.statusBg.hidden = YES;
    self.navigationBar.hidden = YES;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    // 是否允许右滑返回:YES 启动侧滑,NO 禁止侧滑
    [self.navigationController.interactivePopGestureRecognizer setEnabled:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.screenOrientation_rotation_Style = 1;
    
    [self switchNewOrientation:UIInterfaceOrientationPortrait];
}

- (void)clickLeftButton{ // 返回上一页时,再设置 竖屏
    
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.screenOrientation_rotation_Style = 1;
    
    [self switchNewOrientation:UIInterfaceOrientationPortrait];
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation
{
        
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInteger:interfaceOrientation];
        
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    
}

第二种:只适合present 的页面:

原理:在tab和navigationVC(可以自定义一个nav,在这个子nav中)设置:- (UIInterfaceOrientationMask)supportedInterfaceOrientations

tab:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UIViewController *controller = self.selectedViewController;
    if ([controller isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)controller;
        return navigationController.topViewController.supportedInterfaceOrientations;
    } else {
        return controller.supportedInterfaceOrientations;
    }
}

navigationControlller:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.visibleViewController.supportedInterfaceOrientations; 
}

指定横屏的VC:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskLandscape;
    return UIInterfaceOrientationMaskLandscapeRight;
}

相关文章

网友评论

    本文标题:iOS ~ 在全局竖屏的情况下,设置某一个页面强制横屏

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