首页 > 解决方案 > react-native-orientation 库中的 lockToPortrait() 函数出错

问题描述

我正在使用react-native-orientation库使我的应用程序仅以纵向模式打开。这是我的代码:

import React, {Component} from 'react';
import {
    View,
    Text,
} from 'react-native';
import AppContainer from './src/screens/Navigation';
import Orientation from 'react-native-orientation';

const App: () => React$Node = () => {

    useEffect(() => {
        Orientation.lockToPortrait();
    })

    return (
        <AppContainer />
    );
};


export default App;

我在运行此代码时遇到错误。

错误截图

标签: reactjsreact-nativereact-native-android

解决方案


对于 android,您只需将此行添加到 MainActivity 标记中的 AndroidManifest.xml 即可设置方向 android

    <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:screenOrientation="portrait" //add this line for portrait lock
    android:windowSoftInputMode="adjustPan">

在 ios 打开 xcode 文件后执行此操作

在此处输入图像描述


推荐阅读