如何在nativescript中设置方向

xer*_*ant 13 android screen-orientation nativescript

您好我想知道如何在nativescript中设置设备方向.具体来说,我希望我写的应用程序始终保持相同的方向(纵向),以便旋转设备不会导致它进入横向.

我尝试了nativescript-orientation插件和setOrientation.

var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait"); 
Run Code Online (Sandbox Code Playgroud)

但是我收到错误"无法读取属性setOrientation of undefined.tns插件列表显示插件已安装.此外,我尝试删除platforms/android目录并运行tns platform add android相同的结果.

我也尝试将各种组合android:screenOrientation="portrait"放入AndroidManifest.xml但没有成功.

App_resources内部的AndroidManifest.xml看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="__APILEVEL__"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:screenOrientation="portrait"
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/LaunchScreenTheme">
            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

Dea*_* Le 27

我的应用只是纵向.

对于Android,您只需要添加android:screenOrientation="portrait"AndroidManifest.xml里面activity的标签.

对于iOS, open Xcode -> Select project on project navigator (left panel) -> Select target in middle panel -> Choose "General" tab -> Tick only "Portrait" in Deployment info section

  • 对于`iOS`,您可以访问`app/App_Resources/iOS/Info.plist`.在这个文件里面你会找到`<string> UIInterfaceOrientationPortrait </ string>`和Landscape等...只需注释掉或删除你不想支持的方向.无需打开Xcode :) (8认同)

Her*_*sen 6

For iOS: (Without Xcode, just manipulate one file.)

  1. Open file app/App_Resources/iOS/Info.plist
  2. Comment out or delete:

    <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string>

app / App_Resources / iOS / Info.plist

    ...     
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
Run Code Online (Sandbox Code Playgroud)

(支持@mudlabs,他已经在评论中提到了此解决方案。)