shouldAutorotateToInterfaceOrientation在iOS 6中不起作用

时间:2012-09-25 07:09:59

标签: xcode orientation ios6 screen-orientation

iOS 6 shouldAutorotateToInterfaceOrientation中无效但在iOS 5.05.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;    

}

当我搜索这个方向问题时,我找到了所有这些

1 2

但对我没什么用处:( 请帮忙.....

6 个答案:

答案 0 :(得分:51)

编辑:这种情况正在发生,因为Apple改变了管理UIViewController方位的方式。在iOS6中,方向处理方式不同。在 iOS6 shouldAutorotateToInterfaceOrientation方法中已弃用。视图控制器(例如UINavigationController)不会咨询他们的孩子以确定他们是否应该自动旋转。默认情况下,对于iPhone idiom,应用和视图控制器支持的界面方向设置为UIInterfaceOrientationMaskAll,对于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

现在您需要在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的文章。

我希望这很有帮助。

答案 1 :(得分:15)

我解决此问题的方法是在我的应用程序在Delegate Class

中启动时替换以下行
 window addSubview: navigationController.view

window.rootViewController = navigationController

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

答案 2 :(得分:2)

这是因为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);

答案 3 :(得分: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;    
}

答案 4 :(得分:0)

请尝试这个对你有所帮助。

在viewcontroller类中编写代码

-(BOOL)shouldAutorotate
   {
      return YES;
   }

   -(NSUInteger)supportedInterfaceOrientations{
      return UIInterfaceOrientationMaskLandscape;
    }

然后在appdelegate中搜索此行[window addSubview:viewController.view] 并用window.rootViewController = viewController;

替换它

欢呼它会工作。对于ios 6的简单解决方案

答案 5 :(得分: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;
    }