【问题标题】:shouldAutorotateToInterfaceOrientation is not working in iOS 6shouldAutorotateToInterfaceOrientation 在 iOS 6 中不起作用
【发布时间】:2012-09-25 07:09:59
【问题描述】:

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

当我搜索这个方向问题时,我发现的只是thisthis,但对我没有任何帮助:(

请帮忙.....

【问题讨论】:

    标签: xcode orientation ios6 screen-orientation


    【解决方案1】:

    编辑:发生这种情况是因为 Apple 改变了管理 UIViewController 的方向的方式。在 iOS6 中,方向处理方式不同。在 iOS6 中,shouldAutorotateToInterfaceOrientation 方法已被弃用。视图控制器(例如UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转。默认情况下,应用程序和视图控制器支持的界面方向设置为UIInterfaceOrientationMaskAll(适用于 iPad)idiomUIInterfaceOrientationMaskAllButUpsideDown(适用于 iPhone)。

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

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

    更多信息here的一篇关于iOS6.0 Beta 2 OTA的文章。

    我希望这会有所帮助。

    【讨论】:

    • 谢谢伙计!伟大的类别!简单但有用:)
    • 请记住,这是一个 hackish 解决方案 - 强烈建议不要使用 category 覆盖方法stackoverflow.com/questions/5272451/…
    • Yours 是我见过的only 答案,其中提到了关键的委托方法:“willAnimateRotationToInterfaceOrientation”。这允许单独的 viewControllers 通过方向的变化来做特定的事情——就像“shouldAutorotateToInterfaceOrientation”used 做的那样。改变这个有什么用,Apple?
    • [self.viewControllers lastObject]self.topViewController 相同
    • 天哪,没有什么比折旧一个 2 行方法来替换它的几个方法加起来大约 100 行...
    【解决方案2】:

    我解决此问题的方法是在我的应用在委托类中启动时替换以下行

     window addSubview: navigationController.view
    

    window.rootViewController = navigationController
    

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

    【讨论】:

    • 默认情况下,应用程序和视图控制器支持的界面方向设置为 iPad 惯用语的 UIInterfaceOrientationMaskAll 和 iPhone 惯用语的 UIInterfaceOrientationMaskAllButUpsideDown。所以这仅适用于特定的方向,如果你想允许 iphone 的所有方向,你可以像我在回答中所做的那样允许。
    • 我也通过设置 rootViewController 而不是添加子视图来使用它,谢谢老兄。
    【解决方案3】:

    这是因为 Apple 已弃用 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);
    

    【讨论】:

      【解决方案4】:

      我用这个解决了这个问题

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

      【讨论】:

      • 为了您的友好信息,我想告诉此方法- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 在 iOS6 中已弃用。这意味着,将来该方法可以从 iOS SDK 中删除。所以要小心。
      • 是的。你是对的。这种方法将在不久的将来在 iOS6 中被弃用,我正在寻找更好的解决方案。
      • 看看我的回答,伙计。如果您对我的回答有任何问题,请告诉我
      【解决方案5】:

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

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

      -(BOOL)shouldAutorotate
         {
            return YES;
         }
      
         -(NSUInteger)supportedInterfaceOrientations{
            return UIInterfaceOrientationMaskLandscape;
          }
      

      然后在 appdelegate 中搜索这一行 [window addSubview:viewController.view] 并将其替换为 window.rootViewController = viewController;

      干杯它会起作用的。它适用于 ios 6 的简单解决方案

      【讨论】:

        【解决方案6】:

        这对我有用

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多