【问题标题】:Why doesn't my Cordova/PhoneGap iOS app rotate when the device rotates?为什么我的 Cordova/PhoneGap iOS 应用程序在设备旋转时不旋转?
【发布时间】:2012-06-15 11:58:46
【问题描述】:

我正在尝试制作landscape only app,但我根本无法进行任何旋转。

以前在PhoneGap.plist 中有一个autorotate 设置,但在phonegap 1.8.0 中我可以找到它。它还存在吗?

我的应用程序没有旋转还有什么问题?

更新

我知道有一个网页只包含一个单词“test”。我将目标设备设置为仅 iPad 并启用了所有四个方向。还有什么问题?

是否需要特殊的 html 文档类型?我需要包含一些cordova-1.8.0.js吗?我找不到适用于 iOS 的(!?!),所以我用 android 版本对其进行了测试。我读的 API 现在是一样的,所以我可以使用 android .js 文件吗?

【问题讨论】:

    标签: ios cordova screen-orientation


    【解决方案1】:

    我尝试了上面的 JavaScript 解决方案,但没有得到任何乐趣 在 Visual Studio 2015 中,我将 config.xml 更改为

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

    取自Cordova 5 build command is deleting iOS device orientation settings

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

    【讨论】:

    【解决方案2】:

    PiTheNumber 的答案对于那些可以修改 Cordova 生成的本机代码的人来说看起来不错。

    按照 Cordova 上的 this JIRA issue,以及简洁地解释 in this blog,您还可以使用 plist 值或定义一个 window.shouldRotateToOrientation 函数在您的 Javascript 代码中,这非常适合我。

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

    这将为当前页面启用设备方向(因此,对于您的整个应用,如果它是“单页应用”,就像大多数 Cordova 应用一样)。请注意,您还可以根据以度数为单位的旋转值来决定启用它,或者甚至,为什么不启用它,仅在某些视图上启用它,或者让用户在您的 HTML 应用程序中选择......很好,不是吗。

    为了记录,我不需要做任何事情来获得 iOS 8 iPad 手柄旋转,而 iOS 6 和 iOS 7 iPhone 在当前的 Cordova 版本(4.2.0,cordova)中默认不会处理它ios 平台版本“ios 3.7.0”)。这是因为可以在 Xcode 上为每个“设备类型”(平板电脑/手机)授予不同的旋转设置。需要注意的是,Cordova会先检查上面的JS函数是否存在,如果该函数不存在或者不允许旋转,就会使用Xcode的旋转设置。

    【讨论】:

      【解决方案3】:

      Classes/MainViewController.m 返回真:

      - (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。)
      【解决方案4】:

      您可以添加 UISupportedInterfaceOrientations

      platroms/ios/{ProjectName}/{ProjectName-info.plist

      添加以下行:

      对于 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>
      

      【讨论】:

      • 我在别人之前试过这个。第一次像魅力一样工作。没有麻烦。谢谢!这应该是选择的答案!!!太干净了!!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 2012-06-05
      相关资源
      最近更新 更多