shouldAutorotateToInterfaceOrientation在iOS 6中不起作用

在iOS 6 shouldAutorotateToInterfaceOrientation中无效,但在iOS 5.0或中正常工作5.1。


我应该需要改变什么iOS 6。这是我的代码


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if([[[SampleApplicationAppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE])

{

    int nAngle = 0;

    BOOL bRet = NO;


    switch (interfaceOrientation) {

        case UIInterfaceOrientationPortrait:

            nAngle = 90;

            bRet = YES;

            NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);


            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);


            NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);

            break;


        case UIInterfaceOrientationPortraitUpsideDown:

            nAngle = 270;

            bRet = YES;

            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);

            break;


        case UIInterfaceOrientationLandscapeLeft:

            nAngle = 0;

            bRet = YES;

            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);

            break;


        case UIInterfaceOrientationLandscapeRight:

            nAngle = 180;

            bRet = YES;

            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);

            break;


        default:

            break;

    }                

    return bRet;

}    

if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)

    return YES;     

return NO;    

}


当我搜索此定向问题时,我发现了所有这一切


但对我没有任何帮助:(请帮助.....


jeck猫
浏览 768回答 3
3回答

慕侠2389804

之所以发生这种情况,是因为Apple改变了管理Orientation的方式UIViewController。在iOS6中,方向处理方式有所不同。在iOS6中,该 shouldAutorotateToInterfaceOrientation方法已弃用。视图控制器(例如UINavigationController)不咨询其子级来确定是否应自动旋转。默认情况下,UIInterfaceOrientationMaskAll针对iPad 习惯用语和UIInterfaceOrientationMaskAllButUpsideDowniPhone习惯用语,将应用程序和视图控制器的受支持的界面方向设置为。如果要将特定的视图更改为所需的方向,则必须执行某种子类或类别,并重写自动旋转方法以返回所需的方向。将此代码放在您的根视图控制器中。这将有助于UIViewController确定其方向。  //RotationIn_IOS6 is a Category for overriding the default orientation.  @implementation UINavigationController (RotationIn_IOS6) -(BOOL)shouldAutorotate    {      return [[self.viewControllers lastObject] shouldAutorotate];    }  -(NSUInteger)supportedInterfaceOrientations   {     return [[self.viewControllers lastObject] supportedInterfaceOrientations];   } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {     return [[self.viewControllers lastObject]  preferredInterfaceOrientationForPresentation]; } @end现在,您需要在viewController中实现以下方法(iOS6中引入)以进行定向- (BOOL)shouldAutorotate{    //returns true if want to allow orientation change    return TRUE;}- (NSUInteger)supportedInterfaceOrientations{        //decide number of origination tob supported by Viewcontroller.     return UIInterfaceOrientationMaskAll;} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation   {     //from here you Should try to Preferred orientation for ViewController    }并将您的代码放入下面的方法中。无论何时更改设备方向,此方法都将被称为: - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration{if([[[SampleApplicationAppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE]){    int nAngle = 0;    BOOL bRet = NO;    switch (interfaceOrientation) {        case UIInterfaceOrientationPortrait:            nAngle = 90;            bRet = YES;            NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);            NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);            break;        case UIInterfaceOrientationPortraitUpsideDown:            nAngle = 270;            bRet = YES;            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);            break;        case UIInterfaceOrientationLandscapeLeft:            nAngle = 0;            bRet = YES;            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);            break;        case UIInterfaceOrientationLandscapeRight:            nAngle = 180;            bRet = YES;            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);            break;        default:            break;    }                  }    编辑:检查您的窗口,您需要在窗口上添加控制器,rootViewController而不是addSubview如下所示self.window.rootViewController=viewController;有关更多信息,这里是有关iOS6.0 Beta 2 OTA的文章。我希望这可以帮到你。

宝慕林4294392

我解决此问题的方法是,当我的应用程序在Delegate类中启动时替换以下行 window addSubview: navigationController.view与window.rootViewController = navigationController进行此更改后,我的应用开始处理屏幕旋转

慕桂英546537

我解决了这个问题,只是用这个&nbsp; &nbsp; - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(![AppDelegate instance])&nbsp; &nbsp; &nbsp; &nbsp; return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);&nbsp; &nbsp; if([[[AppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int nAngle = 0;&nbsp; &nbsp; &nbsp; &nbsp; BOOL bRet = NO;&nbsp; &nbsp; &nbsp; &nbsp; switch (interfaceOrientation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case UIInterfaceOrientationPortrait:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nAngle = 90;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bRet = YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (order == NSOrderedSame || order == NSOrderedDescending)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // OS version >= 6.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSLog(@"ami iOS 6 er device");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // OS version < 6.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSLog(@"-->%s %d",__FUNCTION__,__LINE__);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case UIInterfaceOrientationPortraitUpsideDown:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nAngle = 270;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bRet = YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (order == NSOrderedSame || order == NSOrderedDescending)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // OS version >= 6.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSLog(@"ami iOS 6 er device");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // OS version < 6.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case UIInterfaceOrientationLandscapeLeft:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nAngle = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bRet = YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case UIInterfaceOrientationLandscapeRight:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nAngle = 180;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bRet = YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return bRet;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)&nbsp; &nbsp; &nbsp; &nbsp; return YES;&nbsp;&nbsp; &nbsp; return NO;&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS