在iOS中反应本机方向

在iOS中反应本机方向,ios,react-native,screen-orientation,Ios,React Native,Screen Orientation,我在React Native中有一个项目,我需要在横向方向中用特定视图锁定方向。但是,代码不起作用,屏幕循环倾斜。我找到了一个与我的问题非常相似的解决方案。但是,这里提出的解决方案不起作用。我已经非常努力地寻找一个解决办法 对不起,英语不好。有人能帮我吗 这是我的密码 import React from 'react'; import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, } fro

我在React Native中有一个项目,我需要在横向方向中用特定视图锁定方向。但是,代码不起作用,屏幕循环倾斜。我找到了一个与我的问题非常相似的解决方案。但是,这里提出的解决方案不起作用。我已经非常努力地寻找一个解决办法

对不起,英语不好。有人能帮我吗

这是我的密码

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import Orientation from 'react-native-orientation-locker';

const App: () => React$Node = () => {
  React.useEffect(() => {
    function _onOrientationDidChange(orientation) {
      if (orientation == 'PORTRAIT') {
        Orientation.lockToLandscapeLeft();
      }
      console.log(orientation);
    }
    Orientation.lockToLandscapeLeft();
    Orientation.addOrientationListener(_onOrientationDidChange);

    //cleanup optional code
    return () => {
      Orientation.unlockAllOrientations();
      Orientation.removeOrientationListener(_onOrientationDidChange);
    };
  });
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <Header />
          {global.HermesInternal == null ? null : (
            <View style={styles.engine}>
              <Text style={styles.footer}>Engine: Hermes</Text>
            </View>
          )}
          <View style={styles.body}>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Step One</Text>
              <Text style={styles.sectionDescription}>
                Edit <Text style={styles.highlight}>App.js</Text> to change this
                screen and then come back to see your edits.
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>See Your Changes</Text>
              <Text style={styles.sectionDescription}>
                <ReloadInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Debug</Text>
              <Text style={styles.sectionDescription}>
                <DebugInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Learn More</Text>
              <Text style={styles.sectionDescription}>
                Read the docs to discover what to do next:
              </Text>
            </View>
            <LearnMoreLinks />
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});

export default App;
从“React”导入React;
进口{
安全区域视图,
样式表,
滚动视图,
看法
文本,
状态栏,
}从“反应本机”;
进口{
标题,
学习重新链接,
颜色,
调试说明,
重新加载指令,
}来自“react native/Libraries/NewAppScreen”;
从“react native Orientation locker”导入方向;
常量应用程序:()=>React$Node=()=>{
React.useffect(()=>{
函数_onOrientationDidChange(方向){
如果(方向==“纵向”){
方向。锁定到左视图();
}
控制台日志(定向);
}
方向。锁定到左视图();
addOrientationListener(_onOrientationDidChange);
//清除可选代码
return()=>{
方向。解锁Allorientations();
方向。移除方向Listener(\u onOrientationDidChange);
};
});
返回(
{global.hermistinternal==null?null:(
引擎:爱马仕
)}
第一步
编辑App.js以更改此设置
屏幕,然后回来查看您的编辑。
查看您的更改
调试
了解更多
阅读文档,了解下一步要做什么:
);
};
const styles=StyleSheet.create({
滚动视图:{
背景颜色:颜色。颜色较浅,
},
发动机:{
位置:'绝对',
右:0,,
},
正文:{
背景颜色:Colors.white,
},
节容器:{
玛金托普:32,
水平方向:24,
},
章节标题:{
尺寸:24,
重量:'600',
颜色:颜色,黑色,
},
章节说明:{
玛金托普:8,
尺码:18,
fontWeight:'400',
颜色:颜色,
},
亮点:{
重量:'700',
},
页脚:{
颜色:颜色,
尺寸:12,
重量:'600',
填充:4,
paddingRight:12,
textAlign:'右',
},
});
导出默认应用程序;

我已根据您的版本配置创建了新的RN项目。这里是App.js的完整代码

"react": "16.9.0",
"react-native": "0.61.3",
"react-native-orientation-locker": "^1.1.7"
工作正常。希望您在pod文件中正确链接了
react native orientation locker

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import Orientation from 'react-native-orientation-locker';

const App: () => React$Node = () => {
  React.useEffect(() => {
   function _onOrientationDidChange(orientation) {
    if (orientation == 'PORTRAIT') {
      Orientation.lockToLandscapeLeft();
    }
    console.log(orientation);
  }
    Orientation.lockToLandscapeLeft();
    Orientation.addOrientationListener(_onOrientationDidChange);

    //cleanup optional code
    return () => {
       Orientation.unlockAllOrientations()
       Orientation.removeOrientationListener(_onOrientationDidChange);
    };
  });
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <Header />
          {global.HermesInternal == null ? null : (
            <View style={styles.engine}>
              <Text style={styles.footer}>Engine: Hermes</Text>
            </View>
          )}
          <View style={styles.body}>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Step One</Text>
              <Text style={styles.sectionDescription}>
                Edit <Text style={styles.highlight}>App.js</Text> to change this
                screen and then come back to see your edits.
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>See Your Changes</Text>
              <Text style={styles.sectionDescription}>
                <ReloadInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Debug</Text>
              <Text style={styles.sectionDescription}>
                <DebugInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Learn More</Text>
              <Text style={styles.sectionDescription}>
                Read the docs to discover what to do next:
              </Text>
            </View>
            <LearnMoreLinks />
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});

export default App;
从“React”导入React;
进口{
安全区域视图,
样式表,
滚动视图,
看法
文本,
状态栏,
}从“反应本机”;
进口{
标题,
学习重新链接,
颜色,
调试说明,
重新加载指令,
}来自“react native/Libraries/NewAppScreen”;
从“react native Orientation locker”导入方向;
常量应用程序:()=>React$Node=()=>{
React.useffect(()=>{
函数_onOrientationDidChange(方向){
如果(方向==“纵向”){
方向。锁定到左视图();
}
控制台日志(定向);
}
方向。锁定到左视图();
addOrientationListener(_onOrientationDidChange);
//清除可选代码
return()=>{
方向。解锁Allorientations()
方向。移除方向Listener(\u onOrientationDidChange);
};
});
返回(
{global.hermistinternal==null?null:(
引擎:爱马仕
)}
第一步
编辑App.js以更改此设置
屏幕,然后回来查看您的编辑。
查看您的更改
调试
了解更多
阅读文档,了解下一步要做什么:
);
};
const styles=StyleSheet.create({
滚动视图:{
背景颜色:颜色。颜色较浅,
},
发动机:{
位置:'绝对',
右:0,,
},
正文:{
背景颜色:Colors.white,
},
节容器:{
玛金托普:32,
水平方向:24,
},
章节标题:{
尺寸:24,
重量:'600',
颜色:颜色,黑色,
},
章节说明:{
玛金托普:8,
尺码:18,
fontWeight:'400',
颜色:颜色,
},
亮点:{
重量:'700',
},
页脚:{
颜色:颜色,
尺寸:12,
重量:'600',
填充:4,
paddingRight:12,
textAlign:'右',
},
});
导出默认应用程序;

在xcode中打开ios项目,然后在project_name.xcodeproj部分中打开

取消选中左横向和右横向


如上图所示。

您能告诉我您的项目react-native和react-native定向锁版本吗?您好,我的react-native版本为0.61.3,react-native定向锁版本为1.1.6。您好,Asha,我尝试复制您的代码,但我的屏幕只旋转了一次。奇怪的是,当我的屏幕是纵向的时,我的控制台日志是横向打印的。嗨@Asha,我尝试了你的代码,但没有得到正确的屏幕行为。我检查依赖项的版本(与您的项目相同),我检查pod文件中的react native orientation locker,并且锁定良好。我尝试在iPhone7模拟器中运行(以及其他