横竖屏切换,控制每一个Controller的横竖屏状态 控制vc 进入的状态横屏或竖屏

横竖屏切换,控制每一个Controller的横竖屏状态

写在前面的话:
在网上看了看,发现大多数博客都写过,但是都会有些小问题。
结合资料和博客总结和整理了一下

适合 ios7+

第一步
首先保证工程支持横竖屏 不多说看图
保证圈红的地方 打对勾
这里写图片描述

第二步
分两种情况
第1中情况。
你的window的rootViewController是一个UITabBarController
这时候你就需要创建一个 继承自 UITabBarController 的YourProjectTabBarController
然后在你的YourProjectTabBarController.m里添加如下的三个方法

//是否跟随屏幕旋转
-(BOOL)shouldAutorotate{
    return self.selectedViewController.shouldAutorotate;
}
//支持旋转的方向有哪些
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}
//控制 vc present进来的横竖屏和进入方向 ,支持的旋转方向必须包含改返回值的方向 (详细的说明见下文)
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

如果YourProjectTabBarController 里装载的视图 又是这种结构的

    UIViewController * vc1 = [[UIViewController alloc]init];
    BaseNavigationViewController * nvc1 = [[BaseNavigationViewController alloc]initWithRootViewController:vc1];

    UIViewController * vc2 = [[UIViewController alloc]init];
    BaseNavigationViewController * nvc2 = [[BaseNavigationViewController alloc]initWithRootViewController:vc2];

    UIViewController * vc3 = [[UIViewController alloc]init];
    BaseNavigationViewController * nvc3 = [[BaseNavigationViewController alloc]initWithRootViewController:vc3];

    self.viewControllers = @[nvc1,nvc2,nvc3];

即 tabbarController的viewControllers里放的是导航栏视图而不是不同的UIViewController
这时候你需要创建一个继承自UINavigationController 的YourProjectNavigationController
然后在你的YourProjectNavigationController.m里添加如下的三个方法

#pragma mark 转屏方法重写
-(BOOL)shouldAutorotate{
    return self.topViewController.shouldAutorotate;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.topViewController supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

仔细看和上面YourProjectTabBarController里的还是有区别的。
一个是  self.selectedViewController 一个是 self.topViewController

接下来就可以控制每一个Controller的是否可以旋转了(前提 “iphone的竖排方向锁定” 是关闭的状态)
在需要旋转的controller里添加如下方法

-(BOOL)shouldAutorotate{
    return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

在不需要旋转的controller里添加如下方法

-(BOOL)shouldAutorotate{
    return NO;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

tips:有一个小建议 你可以创建一个继承自UIViewController的BaseViewController,然后你项目里创建的vc都继承自BaseViewController。你可以在BaseViewController 添加屏幕不能旋转的方法 ,这样创建的每一个vc都不能旋转。 然后,如果你需要旋转的时候,在vc里添加支持旋转的代码就好了。这样就省去了,如果大多数vc不需要支持屏幕旋转,重复添加不支持旋转的代码了。

第2中情况。
你的window的rootViewController是一个UINavigationController
这种情况 你只要省去上面创建 继承自 UITabBarController 的YourProjectTabBarController的步骤就可以了。


详细说下这个方法

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

共有5个返回值
//该方法 只适用于 vc present 进入  不适用于 push 
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            //貌似没什么用
    UIInterfaceOrientationPortrait           //vc 竖屏 从下  present 进入
    UIInterfaceOrientationPortraitUpsideDown //vc 竖屏 从上  present 进入
    UIInterfaceOrientationLandscapeLeft      //vc 横屏 从左  present 进入
    UIInterfaceOrientationLandscapeRight     //vc 横屏 从又  present 进入
} __TVOS_PROHIBITED;

//这个方法需要和下面的方法一起使用
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

//支持的旋转方向必须包含改返回值的方向 这句话的意思是:
举几个例子

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; // 支持竖屏
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft; //却横屏进入
}
这样的组合会造成程序崩溃

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll; // 支持所有状态
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft; //横屏进入
}
或
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape; // 支持横屏状态
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft; //横屏进入
}
这两种就不会崩溃。

该方法主要用于,你当前处于竖屏状态但是你希望下一个页面是 横屏状态 的时候。且 “iphone的竖排方向锁定” 是否开启 不会影响该方法。

猜你喜欢

转载自blog.csdn.net/lwq718691587/article/details/52301059