在iOS 6上,PhoneGap 2.1 iPad应用程序不再自动旋转。

4

在iOS 6上运行应用程序时,我的应用程序不再成功自动旋转。我已经更新到Cordova 2.1,并在我的MainViewController.m文件中添加了以下代码(它是CDViewController子类,以与新的iOS6处理自动旋转方式兼容):

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

// iOS 6
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger ret = 0;

    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait])
        ret = ret | (1 << UIInterfaceOrientationPortrait);
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortraitUpsideDown])
        ret = ret | (1 << UIInterfaceOrientationPortraitUpsideDown);
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight])
        ret = ret | (1 << UIInterfaceOrientationLandscapeRight);
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft])
        ret = ret | (1 << UIInterfaceOrientationLandscapeLeft);

    return ret;
}
3个回答

8
在您的AppDelegate.m文件中,您需要在didFinishLaunchingWithOptions方法中添加以下内容。
[self.window setRootViewController:self.viewController];

一旦您添加此内容,旋转功能应该会重新开始工作。对于我的两个应用程序来说,它已经可以正常使用了。

1
我放置了一行代码,它在iPad 2G上与iOS 6.1Cordova 1.9.0兼容。非常感谢! - newpatriks

4

我在这里还太新了,无法投票支持mattdryden在本帖中的回答。然而,我想要赞同他的回答,并且为了增加价值,指出他建议的修复方法也适用于PhoneGap/Cordova 1.9

我有几个应用程序还没有经过PhoneGap/Cordova 1.9->2.0/2.1更新过程,手动对上面建议的更改进行操作对这些应用程序有效。

另外,放置该行代码的位置似乎很重要。

我最初是在"return YES"之前添加了这一行代码并且失败了。原来你需要将它放置在此行代码之前

[self.window addSubview:self.viewController.view];

还有一件事……为了帮助Google等更快地发现此问题,控制台日志中的一个重要提示是需要添加以下行:

应用程序窗口在应用程序启动时应具有根视图控制器

添加上述代码行可以消除此错误......

希望这能帮助其他遇到此问题的人。


1
我在iOS 6上的iPad应用程序中遇到了类似的问题。这个问题是由于MainViewController没有在AppDelegate.m中设置为rootViewController引起的。请在您的AppDelegate.m中替换以下内容:
[self.window addSubview:self.viewController.view];

使用

self.window.rootViewController = self.viewController;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接