在cordova 3.5.0中iPhone屏幕方向没有变化

时间:2014-07-31 08:48:24

标签: ios iphone cordova screen-orientation

当我使用iPhone在我的应用程序中旋转屏幕时,它始终保持纵向状态且没有任何变化。但它适用于iPad。我可以旋转它并改变方向。

当我转动它时,如何在iPhone中更改我的应用程序屏幕方向?

config.xml 中我有:

<preference name="Orientation" value="default" />

我能找到的唯一解决方案是在cordova代码中的 MainViewController.m 中更改方法 shouldAutorotateToInterfaceOrientation

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

但这对我来说似乎是一个丑陋的解决方案,我真的很想找到正常的做法。

理想情况下,除了portraitUpsideDown之外,iPad和iPhone的所有方向都是好的,但是我不知道我是否允许梦想它。

其他人是否有与iPhone和cordova类似的问题?我已经在Google上搜索了好几天,除了用cordova代码进行黑客攻击外,我找不到任何东西。

3 个答案:

答案 0 :(得分:4)

在深入寻找解决方案之后,我不幸得出结论,这是科尔多瓦问题。

<preference name="Orientation" value="default" />

代码应该实际更改iOS配置文件(/platform/ios/~app_name〜/〜app_name〜.plist)中的平台UISupportedInterfaceOrientations键,以包含以下值:

  • &#34; UIInterfaceOrientationLandscapeLeft&#34 ;;
  • &#34; UIInterfaceOrientationLandscapeRight&#34 ;;
  • &#34; UIInterfaceOrientationPortrait&#34 ;; (我认为)
  • &#34; UIInterfaceOrientationPortraitUpsideDown&#34;

不幸的是,更改config.xml文件并不会改变plist文件,这很烦人。希望这将很快得到排序,通过在线找到的线程来判断,很多人浪费时间寻找解决方案。

我发现最简单的解决方案是手动更改~app_name_.plist文件以包含上述值。我一直在Xcode中这样做,这很容易做到。

希望这会有所帮助。如果有更好的解决方案或Cordova修复此疏忽请告诉我。

答案 1 :(得分:2)

我在XCode中创建了一个使用PListBuddy的构建阶段,这样无论何时运行cordova build ios,它都会运行构建阶段并更新PList文件。关于这一点的好处是,如果您更改config.xml并更改PList,则构建阶段会进入并确保值存在。

我从接受的答案中得到了这个想法:Is there a way of automatically writing custom values to the bundle's .plist during a build phase?

#Grabs info from plist
plist=$SRCROOT"/"$INFOPLIST_FILE
orientations=`/usr/libexec/PlistBuddy -c "Print :UISupportedInterfaceOrientations" "$plist"`

#And changes it before writing out the plist again
if [ "$orientations" ]
then
/usr/libexec/PlistBuddy -c "Delete :UISupportedInterfaceOrientations array" "$plist"
fi
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations array" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationPortrait\"" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationPortraitUpsideDown\"" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationLandscapeLeft\"" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationLandscapeRight\"" "$plist"

作为旁注,我们不应该跳过篮球来吃蛋糕并吃掉它,但无论如何。

答案 2 :(得分:0)

我从这里创建了一个引用的钩子文件: https://stackoverflow.com/a/27063466/4519033

#!/usr/bin/env node

var fs = require('fs');
var plist = 'platforms/ios/<app-name>/<app-name>-Info.plist';

var iphoneModes = [
    "UIInterfaceOrientationLandscapeLeft",
    "UIInterfaceOrientationLandscapeRight",
    "UIInterfaceOrientationPortrait"
];

var ipadModes = [
    "UIInterfaceOrientationLandscapeLeft",
    "UIInterfaceOrientationLandscapeRight"
];

function getOrientationModeStr(modes) {
    var s = "<key>$1</key>\n\t<array>\n\t";
    modes.forEach(function(mode, index) {
        s += "\t<string>"+mode+"</string>\n\t";
    });
    return s;
}

if (fs.existsSync(plist)) {
    var p = fs.readFileSync(plist, 'utf8');
    // replace iphone modes
    p = p.replace(
        /<key>(UISupportedInterfaceOrientations)<\/key>[\r\n ]*<array>[\s\S]*?(?=<\/array>)/ig,
        getOrientationModeStr(iphoneModes)
    );
    // replace ipad modes
    p = p.replace(
        /<key>(UISupportedInterfaceOrientations~ipad)<\/key>[\r\n ]*<array>[\s\S]*?(?=<\/array>)/ig,
        getOrientationModeStr(ipadModes)
    );
    fs.writeFileSync(plist, p, "utf8");
}
希望它有所帮助。

相关问题