shouldAutorotateToInterfaceOrientation不适用于iOS 6

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; 

}

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

1 2

但没有为我工作:(请帮助…..

编辑:这是因为苹果改变了pipe理UIViewController的方向的方式发生。 在iOS6方向处理方式不同。 在iOS6中,shouldAutorotateToInterfaceOrientation方法已被弃用。 视图控制器(如UINavigationController )不要咨询他们的孩子,以确定他们是否应该自动旋转。 默认情况下,应用程序和视图控制器支持的界面方向设置为UIInterfaceOrientationMaskAll为iPad 成语和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; 

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

我希望这是有帮助的。

我解决这个问题的方法是当我的应用程序在Delegate Class中启动时replace下面的行

  window addSubview: navigationController.view 

 window.rootViewController = navigationController 

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

这是因为苹果已经不再使用ios6的应用程序方法,而是使用这些方法

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

我解决了这个问题只是使用这个

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

请尝试这一个,这将有助于你。

在你的viewcontroller类中编写代码

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

然后在appdelegatesearch这一行[window addSubview:viewController.view]并将其replace为window.rootViewController = viewController;

欢呼它将work.its简单的解决scheme为iOS 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; }