在iOS 6中,shouldAutorotateToInterfaceOrientation函数无法正常工作

30

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;    
}
当我搜索这个方向问题时,我发现的是这个这个,但对我都没有用 :( 请帮帮我......
6个回答

50

编辑:发生这种情况是因为苹果已经改变了管理UIViewController方向的方式。在iOS6中,方向处理方式不同。在iOS6中,shouldAutorotateToInterfaceOrientation方法已经被废弃。视图控制器(如UINavigationController)不会查询其子级以确定是否应该自动旋转。默认情况下,iPad平台和iPhone平台的支持接口方向都设置为UIInterfaceOrientationMaskAllButUpsideDown

如果您希望特定的视图更改为所需的方向,则必须创建某种子类或分类,并覆盖自动旋转方法以返回所需的方向。

将此代码放置在您的根视图控制器中。这将帮助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

现在你需要在视图控制器中实现以下方法(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的文章。

希望这对您有所帮助。


谢谢伙计!很棒的类别!简单但有用 :) - danielreiser
3
请记住,这是一种“hackish solution” - 使用类别覆盖方法是强烈不建议的。https://dev59.com/WW435IYBdhLWcg3wrSPN - Łukasz Sromek
你的回答是我见过唯一提到关键委托方法“willAnimateRotationToInterfaceOrientation”的。这使得各个视图控制器可以根据方向变化执行特定操作,就像“shouldAutorotateToInterfaceOrientation”以前所做的那样。苹果改变这个有什么意义呢? - mpemburn
[self.viewControllers lastObject]self.topViewController 是相同的。 - user102008
天啊,没有什么比废弃一个只有两行的方法,然后用几个加起来大约100行的方法来替换它更让人头疼的了... - GeneralMike

15

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

 window addSubview: navigationController.view
使用
window.rootViewController = navigationController

我进行了这个更改之后,我的应用程序开始处理屏幕旋转。


2
默认情况下,应用程序和视图控制器支持的界面方向对于iPad识别为UIInterfaceOrientationMaskAll,而对于iPhone识别为UIInterfaceOrientationMaskAllButUpsideDown。因此,它只适用于特定方向,如果您想允许iPhone的所有方向,则必须像我在答案中所做的那样进行允许。 - Kamar Shad
我也用这个方法,而不是在上面添加子视图,通过设置rootViewController来实现的,谢谢伙计。 - Vishal's p

2
这是因为苹果已经在iOS6中弃用了shouldautorate方法,使用这些方法代替。
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);

1

我用这个方法解决了这个问题

    - (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;    
}

1
提醒您,iOS6中的方法- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation已被弃用。这意味着在未来的iOS SDK版本中可能会移除该方法。因此请注意。 - Kamar Shad
是的,你说得对。这个方法将在不久的将来在iOS6中被弃用,我正在寻找更好的解决方案。 - Saiful
看一下我的答案,伙计。如果我的答案有任何问题,请告诉我。 - Kamar Shad

0

请尝试这个,它会对你有帮助。

在你的视图控制器类中编写代码。

-(BOOL)shouldAutorotate
   {
      return YES;
   }

   -(NSUInteger)supportedInterfaceOrientations{
      return UIInterfaceOrientationMaskLandscape;
    }

在AppDelegate中搜索这一行代码[window addSubview:viewController.view],并将其替换为window.rootViewController = viewController;
非常简单的解决方案,适用于iOS 6。

0

这对我有用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


    - (BOOL)shouldAutorotate {

        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        if (orientation == UIInterfaceOrientationPortrait) {
            // your code for portrait mode

        }

        return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接