iOS 6 shouldAutorotate:没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12775265/
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
iOS 6 shouldAutorotate: is NOT being called
提问by Lizza
I have been scouring the internet for a solution to this but am finding nothing. I am trying to make my iOS 5 app iOS 6 compatible. I cannot get the orientation stuff to work right. I am unable to detect when a rotation is about to happen. Here is the code I am trying:
我一直在互联网上寻找解决方案,但一无所获。我正在尝试使我的 iOS 5 应用程序与 iOS 6 兼容。我无法让方向的东西正常工作。我无法检测到何时即将发生轮换。这是我正在尝试的代码:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
The new supportedInterfaceOrientation: method gets called just fine. The shouldAutorotate method, however, will not fire. I need to do some image swapping on rotate, but I can't get any indication that a rotation is about to occur.
新的 supportedInterfaceOrientation: 方法被调用得很好。但是,shouldAutorotate 方法不会触发。我需要在旋转时进行一些图像交换,但我无法得到任何即将发生旋转的迹象。
Thanks in advance.
提前致谢。
回答by user1672376
See if you are getting the following error when your App starts.
查看您的应用程序启动时是否出现以下错误。
Application windows are expected to have a root view controller at the end of application launch
应用程序窗口应该在应用程序启动结束时有一个根视图控制器
If so the way to fix it is by making the following change in the AppDelegate.m
file (although there seem to be a number of answers how to fix this):
如果是这样,修复它的方法是在AppDelegate.m
文件中进行以下更改(尽管似乎有许多解决方法):
// Replace
[self.window addSubview:[navigationController view]]; //OLD
// With
[self.window setRootViewController:navigationController]; //NEW
After this shouldAutoRotate
should be correctly called.
在这之后shouldAutoRotate
应该正确调用。
回答by Mike Pollard
When using UINavigationController as the basis for an app I use the following subclass to give me the flexibility to allow the top most child viewcontroller to decide about rotation.
当使用 UINavigationController 作为应用程序的基础时,我使用以下子类来灵活地允许最顶层的子视图控制器决定旋转。
@interface RotationAwareNavigationController : UINavigationController
@end
@implementation RotationAwareNavigationController
-(NSUInteger)supportedInterfaceOrientations {
UIViewController *top = self.topViewController;
return top.supportedInterfaceOrientations;
}
-(BOOL)shouldAutorotate {
UIViewController *top = self.topViewController;
return [top shouldAutorotate];
}
@end
回答by borrrden
That method is not the correct way to determine that. The correct method is willRotateToInterfaceOrientation:duration:
该方法不是确定这一点的正确方法。正确的方法是willRotateToInterfaceOrientation:duration:
The should rotate to orientation (as opposed to shouldAutorotate) method is deprecated and will no longer be called as of iOS 6, but it was not meant to be used the way you were using it anyway.
应该旋转到方向(相对于 shouldAutorotate)方法已被弃用,并且从 iOS 6 开始将不再被调用,但无论如何它并不意味着按照您使用它的方式使用它。
EDITResponse to repeated downvotes. Please explain why using the method I indicated is not an (to quote OP) "indication that a rotation is about to occur." The content of the question and the title are mismatched.
编辑对重复downvotes的响应。请解释为什么使用我指出的方法不是(引用 OP)“即将发生轮换的迹象”。问题的内容和标题不匹配。
回答by Adrian
It looks that on iOS 6 the container navigations controller doesn't consult child view controllers when rotating:
看起来在 iOS 6 上,容器导航控制器在旋转时不会咨询子视图控制器:
Now, iOS containers (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.
现在,iOS 容器(例如 UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转。默认情况下,应用程序和视图控制器支持的界面方向设置为 UIInterfaceOrientationMaskAll(对于 iPad 习惯用法)和 UIInterfaceOrientationMaskAllButUpsideDown(对于 iPhone 习惯用法)。
This behavior is easy to test. What I did is to use the same custom view controller
这种行为很容易测试。我所做的是使用相同的自定义视图控制器
- first case as a main view controller
- second case as a child of a UIPageViewController
- 第一种情况作为主视图控制器
- 第二种情况作为 UIPageViewController 的孩子
In the first case everything is decided in the custom navigation controller by the combination of shouldAutorotate
and supportedInterfaceOrientations
given that supportedInterfaceOrientations
agrees with the supported orientations of the application.
在第一种情况下,一切都在自定义导航控制器中通过组合shouldAutorotate
和supportedInterfaceOrientations
给定supportedInterfaceOrientations
与应用程序支持的方向一致来决定。
In the second case even if the supportedInterfaceOrientations
of the custom view controller is called by the UIPageViewController the return value is not taken in to consideration. It works if the two methods are overwritten in a subclass of the UIPageViewController. I am not sure about the side effects of that as this class is not supposed to be subclassed.
在第二种情况下,即使supportedInterfaceOrientations
UIPageViewController 调用自定义视图控制器的 ,也不考虑返回值。如果这两个方法在 UIPageViewController 的子类中被覆盖,它就会起作用。我不确定它的副作用,因为这个类不应该被子类化。
回答by Basheer_CAD
If your viewController
is a child viewController
in a UINavigationController
then you can do the following:
如果您viewController
是一个孩子viewController
,UINavigationController
那么您可以执行以下操作:
- Subclass
UINavigationController
- override
shouldAutoRotate
in your subclass - send your
topViewController
this message when this method get called
- 子类
UINavigationController
shouldAutoRotate
在您的子类中覆盖topViewController
调用此方法时发送此消息
// This Method is inside your UINavigationController
subclass
// 这个方法在你的UINavigationController
子类中
- (BOOL)shouldAutorotate
{
if([self.topViewController respondsToSelector:@selector(shouldAutorotate)])
{
return [self.topViewController shouldAutorotate];
}
return NO;
}
- Now your viewControllers will respond respectively to this method.
- Note, that you can do the same with other orinetaion-methods
- 现在您的 viewController 将分别响应此方法。
- 请注意,您可以对其他 orietaion-methods 执行相同操作
回答by Golden Thumb
I'm using iOS 7 but I believe my case may be helpful to others.
我使用的是 iOS 7,但我相信我的案例可能对其他人有帮助。
I have a deep view controller hierarchy rooted with UITabBarController. The only place where shouldAutorotate is guaranteed to be called is inside the UITabBarController. So I simply subclass UITabBarController and put my rotation control logic inside my shouldAutorotate method.
我有一个以 UITabBarController 为根的深层视图控制器层次结构。唯一保证调用 shouldAutorotate 的地方是在 UITabBarController 内部。所以我只是将 UITabBarController 子类化,并将我的旋转控制逻辑放在我的 shouldAutorotate 方法中。
回答by Michael Jay
I was also getting the following error when your App starts.
当您的应用程序启动时,我也收到以下错误。
"Application windows are expected to have a root view controller at the end of application launch"
“应用程序窗口应该在应用程序启动结束时有一个根视图控制器”
I am using a UISplitViewController *splitViewController
我正在使用 UISplitViewController *splitViewController
If so the way to fixed it is by making the following change in the AppDelegate.m file:
如果是这样,修复它的方法是在 AppDelegate.m 文件中进行以下更改:
Replace
代替
[self.window addSubview:[splitViewController view]];
With
和
[self.window setRootViewController:splitViewController];
After this shouldAutoRotate
was called and worked correctly.
在这shouldAutoRotate
被调用并正常工作之后。
回答by Vladyslav Panchenko
If you use UINavigationController as the basis for an app. I create a category of UINavigationController and call it "UINavigationController+autoRotate". Put this in your UINavigationController+autoRotate.h:
如果您使用 UINavigationController 作为应用程序的基础。我创建了一个 UINavigationController 类别并将其称为“UINavigationController+autoRotate”。把它放在你的 UINavigationController+autoRotate.h 中:
#import <UIKit/UIKit.h>
@interface UINavigationController (autoRotate)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
@end
Put this in UINavigationController+autoRotate.m:
把它放在 UINavigationController+autoRotate.m 中:
#import "UINavigationController+autoRotate.h"
@implementation UINavigationController (autoRotate)
- (BOOL)shouldAutorotate
{
return [self.visibleViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
@end
回答by Sam B
This is how I would do it
这就是我要做的
if you want to check which is current orientation then add this line to your viewconrtoller.m file
如果您想检查哪个是当前方向,则将此行添加到您的 viewconrtoller.m 文件中
#define isPortrait [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown
then where you want to check orientation ,write condition like below
然后你想检查方向,写下条件如下
- (void)viewDidLoad
{
if(isPortrait)
{
//portrait mode....
NSLog(@"its in IsPotraitMode");
}
else
{
//landscape mode....
NSLog(@"its in IsLandscapeMode");
}
}