Typescript 如何在react native中解锁一个屏幕上的旋转

Typescript 如何在react native中解锁一个屏幕上的旋转,typescript,react-native,rotation,screen-rotation,Typescript,React Native,Rotation,Screen Rotation,我尝试在webview中使用react native方向,以使其成为唯一可以旋转的视图 import React, {useEffect} from 'react'; import { WebView } from 'react-native-webview'; import Orientation from "react-native-orientation" export function GameWebViewScreen({navigation}) { const link = ***

我尝试在webview中使用react native方向,以使其成为唯一可以旋转的视图

import React, {useEffect} from 'react';
import { WebView } from 'react-native-webview';
import Orientation from "react-native-orientation"

export function GameWebViewScreen({navigation}) {
const link = ****

useEffect(() => {
    Orientation.unlockAllOrientations();
}, [])

return <WebView source={{uri: link}}/>
}
import React,{useffect}来自“React”;
从“react native WebView”导入{WebView};
从“反应本机方向”导入方向
导出函数GameWebViewScreen({navigation}){
常量链接=****
useffect(()=>{
方向。解锁Allorientations();
}, [])
返回
}
我在unlockAllOrientations调用中得到TypeError:not对象中为null。是否有人知道这是一个问题,因为我还没有按照说明中的说明配置本机文件,因为我还没有访问它们的权限

我还尝试了类组件,得到了相同的结果


我也愿意听取关于控制单视图旋转的库的任何其他建议。

您需要按照说明设置本机文件并尝试这样做

在React本机应用程序中侦听设备方向的更改,并在每个屏幕上以编程方式设置首选方向。适用于Android和iOS

示例:-

import Orientation from 'react-native-orientation'

componentDidMount() {
   Orientation.unlockAllOrientations();
}

componentWillUnmount() {
   Orientation.lockToPortrait();
}
您需要在ios中设置app delegate,如下所示

#import "Orientation.h"

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  return [Orientation getOrientation];
}
安卓需要设置定向软件包

在MainActivity.java中实现onConfigurationChanged方法

import android.content.Intent; // <--- import 
import android.content.res.Configuration; // <--- import 

public class MainActivity extends ReactActivity {
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
     Intent intent = new Intent("onConfigurationChanged");
     intent.putExtra("newConfig", newConfig);
     this.sendBroadcast(intent);
  }
}

import android.content.Intent;// 您需要按照说明设置本机文件并尝试这样做

在React本机应用程序中侦听设备方向的更改,并在每个屏幕上以编程方式设置首选方向。适用于Android和iOS

示例:-

import Orientation from 'react-native-orientation'

componentDidMount() {
   Orientation.unlockAllOrientations();
}

componentWillUnmount() {
   Orientation.lockToPortrait();
}
您需要在ios中设置app delegate,如下所示

#import "Orientation.h"

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  return [Orientation getOrientation];
}
安卓需要设置定向软件包

在MainActivity.java中实现onConfigurationChanged方法

import android.content.Intent; // <--- import 
import android.content.res.Configuration; // <--- import 

public class MainActivity extends ReactActivity {
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
     Intent intent = new Intent("onConfigurationChanged");
     intent.putExtra("newConfig", newConfig);
     this.sendBroadcast(intent);
  }
}
导入android.content.Intent//