竖屏的应用允许单个页面横屏展示的方法!

最近在做一个只支持竖屏的项目,突然遇到了一个需要横屏展示的页面,以下是我查阅资料后实现的方法,这里做个记录方便以后自己和大家使用。

只需要设置三个地方,一个是AppDelegate文件,一个是需要横屏的控制器文件,这里假设为RotationVC,最后是RotationVC的上级页面假设为PreviousVC。

步骤:

1.

1.1在AppDelegate.h文件添加属性@property(assign,nonatomic)BOOL restrictRotation;   

1.2在AppDelegate.m文件添加以下方法

#pragma mark --允许转向

-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window

{

if(self.restrictRotation==YES)

return UIInterfaceOrientationMaskLandscapeRight;

else

return UIInterfaceOrientationMaskPortrait;

}

2.

2.1 在RotaionVC.m中添加如下方法

/**

*设置屏幕旋转

*

*@param restriction yes or no

*/

- (void)restrictRotation:(BOOL) restriction {

AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;

appDelegate.restrictRotation= restriction;

}

2.2

在viewDidLoad方法中添加如下代码

[self restrictRotation:YES];

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];

[[UIDevice currentDevice]setValue:value forKey:@"orientation"];

*3.

最后是PreviousVC。添加如下代码

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

//竖屏

NSNumber*value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];

[[UIDevice currentDevice]setValue:value forKey:@"orientation"];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容