ios强制横屏

需求是,我们的工程中大部分页面都是不支持横屏的,但是某一个页面需要强制横屏,不管用户是否开启了屏幕锁定,到这个页面都强制将屏幕转过来,实现方法如下

1、在工程中选择要支持的方向

2、我们的工程师navigationController+tabbar+vc这样的形式

在要实现屏幕横屏的页面加入以下

//强制屏幕为横屏
-(void)viewWillAppear:(BOOL)animated{
//    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
//    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
-(void)viewWillDisappear:(BOOL)animated{
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}
3、整个工程是由导航tabbar控制的,关键代码粘一下,其中

EHZMainTabBarController是继承自UITabBarController

EHZMainTabBarController *homeTabBarVC = [[EHZMainTabBarController alloc] init];
    self.window.rootViewController = homeTabBarVC;
在EHZMainTabBarController中定义工程结构大致如下
UIViewController *homeVC = [[EHZHomeViewController alloc] init];
    homeVC.hidesBottomBarWhenPushed = NO;
    EHZBaseNavigationController *homeNavigationVC = [[EHZBaseNavigationController alloc] initWithRootViewController:homeVC];
self.viewControllers = @[homeNavigationVC, consultationNavigationVC, discussNavigationVC, personalNavigationVC];


EHZBaseNavigationController是继承自UINavigationController

4、在EHZMainTabBarController和EHZBaseNavigationController重写以下方法

-(BOOL)shouldAutorotate{
    if ([EHZCommonUtil canRotateView]) {
        return YES;
    }
    return NO;
//    return EHZRootNavigationController.topViewController.shouldAutorotate;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([EHZCommonUtil canRotateView]) {
        return EHZRootNavigationController.topViewController.supportedInterfaceOrientations;;
    }
    return UIInterfaceOrientationMaskPortrait;

}
5、canRotateView这个方法我专门抽出来谢了一个方法,供全局判断使用,其中 EHZSignNameViewController是我需要强制横屏的页面

+ (BOOL)canRotateView{
    if ([EHZRootNavigationController.topViewController isKindOfClass:[EHZSignNameViewController class]]){
        return YES;
}else
    return NO;
}

以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值