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

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

作者: turning_blue | 来源:发表于2017-01-16 10:31 被阅读0次

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

只需要设置三个地方,一个是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"];

}

相关文章

网友评论

      本文标题:竖屏的应用允许单个页面横屏展示的方法!

      本文链接:https://www.haomeiwen.com/subject/xhjesttx.html