为了账号安全,请及时绑定邮箱和手机立即绑定

shouldAutorotateToInterfaceOrientation在iOS 6中不起作用

/ 猿问

shouldAutorotateToInterfaceOrientation在iOS 6中不起作用

iOS
jeck猫 2019-11-14 10:15:52

在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;    

}


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


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


查看完整描述

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的文章。


我希望这可以帮到你。


查看完整回答
反对 回复 2019-11-14
?
宝慕林4294392

我解决此问题的方法是,当我的应用程序在Delegate类中启动时替换以下行


 window addSubview: navigationController.view


window.rootViewController = navigationController

进行此更改后,我的应用开始处理屏幕旋转


查看完整回答
反对 回复 2019-11-14
?
慕桂英546537

我解决了这个问题,只是用这个


    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    

    if(![AppDelegate instance])

        return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);


    if([[[AppDelegate 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);


                NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];

                if (order == NSOrderedSame || order == NSOrderedDescending)

                {

                    // OS version >= 6.0

                    NSLog(@"ami iOS 6 er device");

                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);

                }

                else

                {

                    // OS version < 6.0

                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);

                }


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

                NSLog(@"-->%s %d",__FUNCTION__,__LINE__);

                break;

            }


            case UIInterfaceOrientationPortraitUpsideDown:

            {


                nAngle = 270;

                bRet = YES;

                NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];

                if (order == NSOrderedSame || order == NSOrderedDescending)

                {

                    // OS version >= 6.0

                    NSLog(@"ami iOS 6 er device");

                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);

                }

                else

                {

                    // OS version < 6.0

                    _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;    

}


查看完整回答
反对 回复 2019-11-14

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信