shouldAutorotateToInterfaceOrientation在iOS 6中不起作用

Sai*_*aif 29 xcode orientation screen-orientation ios6

In 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;    
Run Code Online (Sandbox Code Playgroud)

}

当我搜索这个方向问题时,我发现了这一切

1 2

但没有什么对我有用:(请帮助.....

Kam*_*had 51

编辑:这种情况正在发生,因为Apple改变了管理方向的方式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
Run Code Online (Sandbox Code Playgroud)

现在,您需要在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 
   }
Run Code Online (Sandbox Code Playgroud)

并将您的代码放在下面的方法中.每当设备方向改变时,将调用此方法:

 - (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;
    }                
  }    
Run Code Online (Sandbox Code Playgroud)

编辑:检查你的窗口,你需要在窗口上添加控制器rootViewController而不是addSubview像下面那样

self.window.rootViewController=viewController;
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,这里是关于iOS6.0 Beta 2的OTA的文章.

我希望这可以帮到你.

  • 请记住,这是一个hackish解决方案 - 强烈建议不要使用类别覆盖方法http://stackoverflow.com/questions/5272451/overriding-methods-using-categories-in-objective-c (3认同)

小智 15

我修复此问题的方法是在我的应用程序在Delegate Class中启动时替换以下行

 window addSubview: navigationController.view
Run Code Online (Sandbox Code Playgroud)

window.rootViewController = navigationController
Run Code Online (Sandbox Code Playgroud)

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

  • 默认情况下,应用程序和视图控制器支持的界面方向设置为iPad惯用语的UIInterfaceOrientationMaskAll和iPhone惯用语的UIInterfaceOrientationMaskAllButUpsideDown.这样只有特定的方向,如果你想允许我允许的所有iphone方向,就像我在答案中所做的那样. (2认同)