为什么我的Cordova/PhoneGap iOS应用在设备旋转时无法旋转?

11
我正在尝试制作一个仅限横屏应用程序,但我无法旋转任何内容。
PhoneGap.plist中曾经有一个autorotate设置,但是在phonegap 1.8.0中我找不到它。它还存在吗?
我的应用程序为什么不能旋转?还可能有什么其他问题? 更新 我现在有一个只包含一个单词“test”的网页。我将目标设备设置为仅限iPad,并启用了所有四个方向。仍然可能有什么问题?
我需要特殊的HTML文档类型吗?我需要包含一些cordova-1.8.0.js吗?我找不到iOS的文件,所以我用android版本进行了测试。我读到API现在是相同的,所以我可以使用android.js文件吗?
4个回答

15

我尝试了上面的JavaScript解决方案,但没有成功。 在Visual Studio 2015中,我将config.xml更改为

<preference name="orientation" value="all" />

取自Cordova 5构建命令正在删除iOS设备方向设置

我不需要JavaScript,只需要配置设置


不幸的是,这在 Cordova 5.4 中已经失效了:https://issues.apache.org/jira/browse/CB-9975 - user206481
name="orientation" 没有起作用,**name="Orientation"**(注意大写字母 O)似乎是正确的。 - shershen

11

PiTheNumber的回答对于那些习惯于修改Cordova生成的本地代码的人来说看起来不错。

在Cordova上,可以遵循这个JIRA问题这篇博客所清晰解释的方法,您还可以使用plist值或在您的Javascript代码中定义window.shouldRotateToOrientation函数,这非常适合我。

window.shouldRotateToOrientation = function(degrees) {
 return true;
}

这将为当前页面启用设备方向(因此,如果它是“单页应用程序”,则为整个应用程序)。请注意,您还可以决定基于旋转角度值启用它,甚至仅在某些视图上启用它,或者让用户在您的HTML应用程序中进行选择...很不错,不是吗。
值得一提的是,在当前Cordova版本(4.2.0,cordova ios平台版本“ios 3.7.0”)中,我不需要做任何事情就能使iOS 8 iPad处理旋转,而iOS 6和iOS 7 iPhone默认情况下无法处理它。这是因为可以在Xcode上针对“设备类型”(平板电脑/手机)授予不同的旋转设置。需要注意的是,Cordova将首先检查上面的JS函数是否存在,然后如果该函数不存在或未允许旋转,则将使用Xcode旋转设置。

8

Classes/MainViewController.m 中返回 true:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return true;
}

对于 iOS >= 6

- (BOOL)shouldAutorotate {
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

Source


这对我解决了问题。(对于我来说,在默认的PhoneGap项目中,旋转在iPad上可以工作,但在iPhone上无法工作。) - mpontillo

7
你可以在 platroms/ios/{ProjectName}/{ProjectName-info.plist 中添加 UISupportedInterfaceOrientations,添加以下行:

对于 iPhone:

<key>UISupportedInterfaceOrientations</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>

针对 iPad:

<key>UISupportedInterfaceOrientations~ipad</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>

我在尝试其他方法之前就尝试了这个。第一次就非常顺利。没有任何麻烦。谢谢!这应该是被选择的答案!!!太棒了!!! - moeiscool

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