美文网首页
iOS强制横屏

iOS强制横屏

作者: 第九号线 | 来源:发表于2017-12-26 17:09 被阅读26次

项目有一个需求:项目中某一个视图控制器需要强制横屏(或竖屏),并且不能转屏;其他的控制器横竖屏都可以。
查了很多资料,遇到很多坑。大部分网友提供的方法都不起作用;有一些起作用的,但是BUG太多,经不起反复调试。
所以自己总结了一套方法,暂时解决了问题。
如果发现问题,请轻喷。

上代码:

//AppDelegate.m

//在AppDelegate中设置是否允许转屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        if (self.allowRotation) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }else {
            //在iPad时,强制横屏
            return UIInterfaceOrientationMaskLandscape;
        }
    }else {
        if (self.allowRotation) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }else {
            //在iPhone时,强制竖屏
            return UIInterfaceOrientationMaskPortrait;
        }
    }
}

添加一个分类: UIViewController+ForceOrientation

//UIViewController+ForceOrientation.m

#pragma mark - public

- (void)updateOrientationLandscapeOrPortrait:(BOOL)animation {
    //不允许转屏
    [self forceOrientationLandscapeOrPortrait];
    
    //当iPad时,且不是右侧横屏(UIInterfaceOrientationLandscapeRight),强制横屏,并刷新界面
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationLandscapeRight) {
            
            //关键:强制横屏,Home键在右边
            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
            //刷新控制器。否则,[UIScreen mainScreen].bounds的size不是你期望的;self.view.frame/bounds都不是期望的。
            [UIViewController attemptRotationToDeviceOrientation];
        }
        
    }else {
        if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait) {
            //关键:强制竖屏
            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
            //刷新控制器。
            [UIViewController attemptRotationToDeviceOrientation];
        }
    }
}

- (void)updateOrientationAll:(BOOL)animation {
    //允许横屏和竖屏
    [self forceOrientationAll];
}

#pragma mark - 是否允许转屏

/**
 * 不允许转屏
 */
- (void)forceOrientationLandscapeOrPortrait {
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //不允许再转屏
    appDelegate.allowRotation = false;
    [appDelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

/**
 * 允许转屏
 */
- (void)forceOrientationAll {
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //允许转屏
    appDelegate.allowRotation = true;
    [appDelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

然后测试:

//WYCellViewController.m

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
 
    [self updateOrientationLandscapeOrPortrait:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [self updateOrientationAll:animated];
}

传送门

完。

相关文章

  • iOS强制横屏

    iOS强制横屏

  • iOS 强制横屏(Push和模态)

    # iOS 强制横屏(Push和模态) iOS开发过程中,有时候需要页面强制横屏。 下面这种方法是不管手机有没有开...

  • iOS强制横屏

    朋友给的场景,一个vc present一个nav包着一个vc,这个被present出来的vc要求横屏,dismi...

  • iOS强制横屏

    前言 最近在项目中有一个需求,就是进入某一个页面时要强制横屏,其他界面不做限制,查了资料发现有些方法对我的需求只有...

  • iOS 强制横屏

    NSNumber *orienTarget=[NSNumber numberWithInt: UIInterfac...

  • iOS强制横屏

    项目有一个需求:项目中某一个视图控制器需要强制横屏(或竖屏),并且不能转屏;其他的控制器横竖屏都可以。查了很多资料...

  • iOS强制横屏

    1.http://www.cnblogs.com/block123/p/5917770.html 2.http:/...

  • ios强制横屏

    在强制横屏的页面重新加载init方法:(ios8后会隐藏状态栏) ios8 以后横屏状态栏不显示 解决方法:

  • iOS9之后强制横屏

    1、IOS8之后有的方法写到类里强制横屏之后已经没有用了 2、IOS8之后该怎么实现强制横屏 首先在代理类实现该方...

  • iOS 强制转屏 强制横屏 强制竖屏

    今天项目中遇到正在看视频的时候账号被挤,如果当时是横屏的情况下,需要强制竖屏。真头疼,网上找了好多方法,终于解决啦...

网友评论

      本文标题:iOS强制横屏

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