iOS单一页面横屏显示

直接来代码


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;


3.最后在如果横屏页是被push出来的 在前以页面强制竖屏一下:

- (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
发布了47 篇原创文章 · 获赞 15 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/gavin__fan/article/details/52622019