iOS项目不支持横屏的前提下某个界面横竖屏(旋转)的解决方案

来源:互联网 发布:mcu和单片机的区别 编辑:程序博客网 时间:2024/05/16 06:16

项目需求(场景): 整个项目不需要旋转(当然了,所有方向的屏幕适配都做好的情况下是没问题的,但是没有必要这么劳民伤财。)的前提下,播放界面控制器需要支持屏幕的其他方向的旋转。

解决方案:(分为四步)

步骤1、

设置
targets——>general——>device Orientation

截图

步骤2、

 <1>在 AppDelegate.h 里面 加一个属性 
@property (nonatomic,assign)BOOL allowRotation;//这个属性标识屏幕是否允许旋转
<2>在 AppDelegate.m 里面
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    if (self.allowRotation) {//当允许时,支持所有方向        return UIInterfaceOrientationMaskAll;    }    //否则 就只有竖屏    return UIInterfaceOrientationMaskPortrait;}

步骤3、
在你想要支持 旋转的控制器 需要导入#import "AppDelegate.h"

    - (void)viewWillAppear:(BOOL)animated {        [super viewWillAppear:animated];        self.navigationController.navigationBarHidden = YES;    //改变AppDelegate的appdelegete.allowRotation属性     AppDelegate *appdelegete = (AppDelegate *)[UIApplication sharedApplication].delegate;    appdelegete.allowRotation = YES;}
- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    self.navigationController.navigationBarHidden = NO;    AppDelegate *appdelegete = (AppDelegate *)[UIApplication sharedApplication].delegate;    appdelegete.allowRotation = NO;}

步骤4、当你写完这些的时候发现好像可以了,还有问题。
但你横屏播放的状态下 直接返回上个控制器(pop)的时候.
发现那个控制器也是横屏的(当你竖屏时,它会转过来,但是再旋转就不行了。问题就是pop回来不能使横屏啊,应该直接竖屏才是啊)
比如pop回的那个控制器叫 TextViewController
TextViewController.m

//屏幕方向操作-(UIInterfaceOrientationMask)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait;}

此方法是UIKit框架中UIViewController.h的公布方法,iOS 6.0后加入

0 0
原创粉丝点击