iOS强制横屏

项目有一个需求:项目中某一个视图控制器需要强制横屏(或竖屏),并且不能转屏;其他的控制器横竖屏都可以。
查了很多资料,遇到很多坑。大部分网友提供的方法都不起作用;有一些起作用的,但是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强制横屏)