iOS指定页面屏幕旋转

来源:互联网 发布:常熟淘宝培训 南天首家 编辑:程序博客网 时间:2024/06/07 22:28

先说下我的需求吧,应用要求不能够旋转,但在视频播放页面要求能够旋转,实现全屏播放,同时在全屏页面调节手机屏幕的亮度和音量大小,当返回(pop)或者跳转(push)到其他页面时,取消屏幕旋转。

第一步:先实现指定页面(视频页面)能够屏幕旋转

1.在工程里面TARGETS –> General 勾选这些选项,我的项目只需要左右旋转,所有不选Upside Down

这里写图片描述

2.创建一个继承于UINavigationController的类就叫CustomNavViewController吧(这是一般操作,在开发项目时一般都会创建这样一个类,方便以后继承并且拓展方法),添加如下方法,其中VideoDetailViewController类就是你要旋转的那么个页面:

- (BOOL)shouldAutorotate{    return YES;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    if ([self.topViewController isKindOfClass:[VideoDetailViewController class]])    {        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;    }    return UIInterfaceOrientationMaskPortrait;}

3.在根视图控制器中(我的根视图控制器就是继承自UITabBarController的类MainsViewController),添加如下方法:

-(BOOL)shouldAutorotate
{
UIViewController * navVC = self.selectedViewController;
if ([navVC isKindOfClass:[CustomNavViewController class]])
{
return YES;
}
return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
UIViewController * vc = self.selectedViewController;
if (vc)
{
return [vc supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIViewController * vc = self.selectedViewController;
if (vc)
{
return [vc preferredInterfaceOrientationForPresentation];
}
return UIInterfaceOrientationPortrait;
}

但是这样从横屏界面pop到上一个界面可能会存在一点小问题,所以还是需要再做一个设置,在其pop回去的那个页面里加上两行代码:
NSNumber * number = [NSNumber numberWithInt:UIInterfaceOrientationMaskPortrait];
[[UIDevice currentDevice] setValue:number forKey:@”orientation”];

原创粉丝点击