直接来代码
1.APPDelegate里
.h
添加属性:
//页面需要横屏时赋值
@property (nonatomic, assign) BOOL isFull;
.m
增加代理方法:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.isFull) {
return UIInterfaceOrientationMaskAll;//适应所有方向
}
return UIInterfaceOrientationMaskPortrait;//竖屏
}
2.然后在需要横屏显示的ViewController里面:
- (void)loadView
{
[super loadView];
//将标示改为YES
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.isFull = YES;
}
注意:此时你pop到前一页时前以页面也是横屏并且可以横竖屏转换,所以在pop出页面前添加:
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.isFull = NO;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Gavin-单页横屏 ESM返回后 强制竖屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
强制竖屏: http://www.jianshu.com/p/6c45fa2bb970