在 iOS 7 中强制横向和自动旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18959740/
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
Forcing landscape and autorotate in iOS 7
提问by ultra
My app is supposed to be landscape only, and I had no problem with this when building for iOS 6and earlier. Now with iOS 7,It won't rotate at all.
我的应用程序应该只是横向的,在为iOS 6及更早版本构建时我没有遇到这个问题。现在使用iOS 7,它根本不会旋转。
In my app settings, I have it set to landscape left/right only. In my view controller, i'm using the following:
在我的应用程序设置中,我将它设置为仅向左/向右横向。在我的视图控制器中,我使用以下内容:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
I also used to use this, which is now deprecated:
我也曾经使用过这个,现在已弃用:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return UIInterfaceOrientationIsLandscape(orientation);
}
The new one appears to be shouldAutorotate, but using this crashes my app. Any ideas on this would be appreciated, since my app is forced to portrait on my iPad and in the simulator. Thank you!
新的似乎是 shouldAutorotate,但使用它会使我的应用程序崩溃。对此的任何想法都将不胜感激,因为我的应用程序被迫在我的 iPad 和模拟器中进行肖像。谢谢!
回答by ultra
This solves my problem. I'm not sure why I had issues before, but I must have missed trying this exact combination (also, info.plist should have the supported orientations set).
这解决了我的问题。我不确定为什么我之前遇到过问题,但我一定错过了尝试这种精确组合的机会(而且 info.plist 应该设置了支持的方向)。
(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
(BOOL)shouldAutorotate {
return YES;
}
edit: I may have having issues with the simulator, and doing a reset/restart and clean might have contributed to the fix.
编辑:我的模拟器可能有问题,并且进行重置/重启和清理可能有助于修复。
回答by Puneet Sharma
Include this method as well in your code:
在您的代码中也包含此方法:
- (BOOL)shouldAutorotate{
if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft ||[[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight)
{
return YES;
}
else{
return NO;
}
}
Read thisfor more info. Here it is mentioned that we should override shouldAutorotate
to suppress orientations.
阅读本文了解更多信息。这里提到我们应该覆盖shouldAutorotate
以抑制方向。
If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate method on the topmost view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.
如果您想暂时禁用自动旋转,请避免操作方向蒙版来执行此操作。相反,覆盖最顶层视图控制器上的 shouldAutorotate 方法。在执行任何自转之前调用此方法。如果它返回 NO,则旋转被抑制。
回答by Siddhartha Moraes
i don't know why, but this work for me on IOS 7
我不知道为什么,但这在 IOS 7 上对我有用
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[super willRotateToInterfaceOrientation:UIInterfaceOrientationPortrait duration:0];
回答by ShanghaiD
I may have having issues with the simulator, and doing a reset/restart and clean might have contributed to the fix.
我的模拟器可能有问题,并且执行重置/重新启动和清理可能有助于修复。
This worked for me: (Simulator -> Reset Content and Settings...)
这对我有用:(模拟器-> 重置内容和设置...)