iOS 个别页面强制横屏,其他页面竖屏

在开发项目的时候,遇到了一个问题,就是其中一个页面需要强制横屏,而其他页面要强制竖屏。

 我的解决方法是这样的。在AppDelegate.h里面添加@property(nonatomic,assign)NSInteger allowRotation;
在AppDelegate.m文件里面添加

 1 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
 2 {
 3     if (_allowRotation == 1) {
 4         return UIInterfaceOrientationMaskLandscapeRight;
 5     }
 6     else
 7     {
 8         return (UIInterfaceOrientationMaskPortrait);
 9     }
10 }

这样默认所以的页面就是竖屏的,在要强制横屏的页面的控制器UIViewController里面,引入#import "AppDelegate.h"

然后

- (void)viewDidLoad
{
    [super viewDidLoad];
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = 1;
}


就可以让个别页面单独横屏了

原文地址:https://www.cnblogs.com/sunkaifeng/p/5191172.html