xcode shouldAutorotateToInterfaceOrientation 在 iOS 6 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12577879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
shouldAutorotateToInterfaceOrientation is not working in iOS 6
提问by Saif
In iOS?6, shouldAutorotateToInterfaceOrientation
is not working, but it worked fine in iOS?5.0 and?5.1.
在 iOS?6 中,shouldAutorotateToInterfaceOrientation
不工作,但在 iOS?5.0 和?5.1 中工作正常。
What do I need to change for iOS?6? Here is my?code:
我需要为 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;
}
When I searched for this orientation problem, all I found was thisand this, but nothing worked for me :(
当我搜索这个方向问题时,我发现的只是这个和这个,但对我来说没有任何作用:(
Please help .....
请帮忙 .....
回答by Kamar Shad
EDIT:This is happening because Apple has changed the way of managing the Orientation of UIViewController
. In iOS6 orientation handles differently. In iOS6shouldAutorotateToInterfaceOrientation
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 idiomand UIInterfaceOrientationMaskAllButUpsideDown
for the iPhone idiom.
编辑:发生这种情况是因为 Apple 改变了管理UIViewController
. 在 iOS6 方向处理不同。在iOS6 中shouldAutorotateToInterfaceOrientation
方法已弃用。视图控制器(例如UINavigationController
)不会咨询他们的孩子来确定他们是否应该自动旋转。默认情况下,应用程序和视图控制器支持的界面方向设置UIInterfaceOrientationMaskAll
为 iPad惯用语和UIInterfaceOrientationMaskAllButUpsideDown
iPhone 惯用语。
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.
如果您希望将特定视图更改为所需的方向,您必须执行某种子类或类别并覆盖自动旋转方法以返回所需的方向。
Place this code in your root view controller. This will help the UIViewController
to determine its orientation.
将此代码放在您的根视图控制器中。这将有助于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
Now you need to implement below methods (introduced in iOS6) in viewController for orientation
现在您需要在 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
}
And put your code inside the below method. Whenever device orientation is changed this 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;
}
}
Edit:check your window, you need to add the controller on window as rootViewController
rather than addSubview
like below
编辑:检查您的窗口,您需要在窗口上添加控制器,rootViewController
而不是addSubview
像下面那样
self.window.rootViewController=viewController;
For more information here's an article about iOS6.0 Beta 2 OTA.
欲了解更多信息,这里的约iOS6.0 Beta 2的OTA的文章。
I hope this was helpful.
我希望这可以帮到你。
回答by RAJ
The way I fixed this problem was to replace the following line when my app starts in Delegate Class
我解决这个问题的方法是在我的应用程序在委托类中启动时替换以下行
window addSubview: navigationController.view
with
和
window.rootViewController = navigationController
After I made this change my app started to handle screen rotations
进行此更改后,我的应用程序开始处理屏幕旋转
回答by Saad
This is because Apple has deprecated shouldautorate method from ios6 use these methods instead
这是因为 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);
回答by Saif
I Solved this problem just use this
我解决了这个问题就用这个
- (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;
}
回答by Shauket Sheikh
please try this one it will be helpful for you.
请试试这个,它会对你有帮助。
write code in you viewcontroller class
在您的视图控制器类中编写代码
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
Then in appdelegate search this line [window addSubview:viewController.view] and replace this with window.rootViewController = viewController;
然后在 appdelegate 中搜索这一行 [window addSubview:viewController.view] 并将其替换为 window.rootViewController = viewController;
cheers it will work.its simple solution for ios 6
欢呼它会起作用。 ios 6 的简单解决方案
回答by skhurams
this worked for me
这对我有用
- (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;
}