问题描述
在 iOS 6 中,shouldAutorotateToInterfaceOrientation
不起作用,但它在 iOS 5.0 和 5.1 中运行良好.
In iOS 6, shouldAutorotateToInterfaceOrientation
is not working, but it worked fine in iOS 5.0 and 5.1.
我需要为 iOS 6 进行哪些更改?这是我的代码:
What do I need to change for iOS 6? Here is my code:
- (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;
}
当我搜索这个方向问题时,我发现的只有这个和这个,但对我没有任何作用:(
When I searched for this orientation problem, all I found was this and this, but nothing worked for me :(
请帮忙.....
推荐答案
这是因为 Apple 改变了管理 UIViewController
的方向的方式.在 iOS6 方向处理不同.在 iOS6 中 shouldAutorotateToInterfaceOrientation
方法已弃用.视图控制器(例如 UINavigationController
)不会咨询他们的孩子来决定他们是否应该自动旋转.默认情况下,对于 iPad 和 UIInterfaceOrientationMaskAllButUpsideDown
适用于 iPhone 惯用语.
This is happening because Apple has changed the way of managing the Orientation of UIViewController
. In iOS6 orientation handles differently. In iOS6 shouldAutorotateToInterfaceOrientation
method is deprecated. View controllers (such as UINavigationController
) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll
for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown
for the iPhone idiom.
如果您希望将特定视图更改为所需的方向,您必须执行某种子类或类别并覆盖自动旋转方法以返回所需的方向.
If you want a specific view to be changed to the desired orientation you have to do some sort of subclass or category and override the autorotation methods to return the desired orientation.
将此代码放在您的根视图控制器中.这将帮助 UIViewController
确定它的方向.
Place this code in your root view controller. This will help the UIViewController
to determine its orientation.
//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中引入)用于方向
Now you need to implement below methods (introduced in iOS6) in viewController for orientation
- (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
}
并将您的代码放在下面的方法中.每当设备方向改变时这个方法将被调用:
And put your code inside the below method. Whenever device orientation is changedthis method will be called:
- (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
check your window, you need to add the controller on window as rootViewController
rather than addSubview
like below
self.window.rootViewController=viewController;
欲了解更多信息这里 是一篇关于 iOS6.0 Beta 2 OTA 的文章.
For more information here's an article about iOS6.0 Beta 2 OTA.
我希望这会有所帮助.
这篇关于shouldAutorotateToInterfaceOrientation 在 iOS 6 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!