单个页面横屏,其余全部竖屏


需求,大部分页面都是树屏模式,部分页面需要横屏,在tabbar基类。Nav基类中添加一下代码,但是这样还是会出现一个问题:在系统横屏时,app打开时会默认横屏展示,解决方案见下:

#import "BaseTabbarController.h"


@interface BaseTabbarController ()

@end

@implementation BaseTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#import "BaseNavController.h"
#import "LiveInteracterViewController.h"
#import "LivePushViewController.h"
#import "MineViewController.h"
@interface BaseNavController ()

@end

@implementation BaseNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if ([self.topViewController isKindOfClass:[LivePushViewController class]]) {
        return UIInterfaceOrientationLandscapeLeft;
    }else if ([self.topViewController isKindOfClass:[MineViewController class]]){
        return UIDeviceOrientationPortrait;
    }
    return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
    if ([self passViewController]) {
        return YES;
    }
    return NO;
}
-(BOOL)passViewController
{
    return [self.topViewController isKindOfClass:[LiveInteracterViewController class]]
            || ([self.topViewController isKindOfClass:[LivePushViewController class]]
            && UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
            || ([self.topViewController isKindOfClass:[MineViewController class]]
            && !UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation));
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if ([self.topViewController isKindOfClass:[LivePushViewController class]]) {
        return UIInterfaceOrientationMaskLandscape;
    }else if ([self.topViewController isKindOfClass:[MineViewController class]]){
        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count) {
        viewController.hidesBottomBarWhenPushed=YES;
    }
    [super pushViewController:viewController animated:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
解决办法:

在APPdelegate中添加下面代码

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

然后General---Device Orientation中只勾选Portrait,CMD+R运行成功



另一种方法:
使用transform属性,此时与重力感应无关,但是状态栏没有横屏(后面再研究)

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
   self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
      self.view.bounds = CGRectMake(0, 0,kScreenHeight, kScreenWidth);

其他:

//set orientation

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];

//refresh

    [UIViewController attemptRotationToDeviceOrientation];



方法三:
创建UITabbarVC的类别

@interface UITabBarController (Rotation)

@end
#import "UITabBarController+Rotation.h"

@implementation UITabBarController (Rotation)
/**
 * 如果window的根视图是UITabBarController,则会先调用这个Category,然后调用UIViewController+Rotation
 * 只需要在支持除竖屏以外方向的页面重新下边三个方法
 */
// 是否支持自动转屏
- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}
// 支持哪些屏幕方向
- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
@end

创建UINavVC

@interface UINavigationController (Rotation)

@end
#import "UINavigationController+Rotation.h"

@implementation UINavigationController (Rotation)
/**
 * 如果window的根视图是UINavigationController,则会先调用这个Category,然后调用UIViewController+ZFPlayerRotation
 * 只需要在支持除竖屏以外方向的页面重新下边三个方法
 */

// 是否支持自动转屏
- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}

// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

- (UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}
@end

创建UIViewVC的分类

@interface UIViewController (Rotation)

@end
#import "UIViewController+Rotation.h"

@implementation UIViewController (Rotation)
/**
 * 默认所有都不支持转屏,如需个别页面支持除竖屏外的其他方向,请在viewController重新下边这三个方法
 */

// 是否支持自动转屏
- (BOOL)shouldAutorotate {
    return NO;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}
@end

默认全部为竖屏在需要旋转屏幕的类中,重写下面的方法即可:

- (BOOL)shouldAutorotate {
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

比如:LiveStreamViewController.h 进入是就需要横屏,,则见一下代码

@implementation LiveStreamViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;//刚进去时,在竖屏切换到全屏瞬间,tabbar会显示,之后消失,此为解决方案
    [self orentationWithScape:UIInterfaceOrientationLandscapeLeft];//设置全屏
}

- (IBAction)backBtnClick:(UIButton *)sender {
    [self orentationWithScape:UIInterfaceOrientationPortrait];//退出时,设置为竖屏,如果不设置那么会导致退出再次进入,不会切换到横屏状态。
    [self.navigationController popViewControllerAnimated:true];
}

-(void)orentationWithScape:(UIInterfaceOrientation)orentation
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:orentation] forKey:@"orientation"];
    [UIViewController attemptRotationToDeviceOrientation];
 }


- (BOOL)shouldAutorotate {
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft;
}


注意:

//手机为横屏模式下打开APP  APP会按照横屏来布局
//
//需要在 如下方法中新加
//
//- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//
//    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];//此方法
//
//    ....
//
//}
//
//这样就会解决横屏模式下打开 APP 产生的一些问题
//
//(情况二 容易出现的一些问题为:当你在didFinishLaunchingWithOptions中 加载了另一个 window 的时候需要在自己建的 window 中也遵守屏幕旋转的几个代理方法 不然横屏模式下打开APP布局依然会乱)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值