shouldAutorotateToInterfaceOrientation在iOS 6中不起作用

问题描述 投票:30回答: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;    
}

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

请帮助.....

xcode orientation ios6 screen-orientation
6个回答
51
投票

EDIT:之所以发生这种情况,是因为Apple已更改了管理UIViewController方向的方式。在iOS6中,方向处理方式有所不同。在iOS6中,不推荐使用shouldAutorotateToInterfaceOrientation方法。视图控制器(例如UINavigationController)不咨询其子级来确定是否应自动旋转。默认情况下,将应用程序和视图控制器支持的界面方向设置为UIInterfaceOrientationMaskAll(对于iPad idiom)和UIInterfaceOrientationMaskAllButUpsideDown(对于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的文章。

我希望这会有所帮助。


15
投票

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

 window addSubview: navigationController.view

with

window.rootViewController = navigationController

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


3
投票

这是因为Apple已从ios6中弃用了应该使用autoautorate方法,而改用这些方法

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

0
投票

请尝试这一步骤,对您有所帮助。

在您的viewcontroller类中编写代码

-(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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.