React-Native + Flex不响应方向更改

Pri*_*ank 15 ios flexbox react-native

我正在使用React-Native编写通用iPhone/iPad应用程序.但是当方向改变时,我正在努力正确地渲染我的视图.以下是js文件的源代码:

'use strict';
    var React = require('react-native');

    var {
      Text,
      View
    } = React;

    var CardView = require('./CardView');

    var styles = React.StyleSheet.create({
      container:{
        flex:1,
        backgroundColor: 'red'
      }
    });

    class MySimpleApp extends React.Component {
      render() {
         return <View style={styles.container}/>;
      }
    }

    React.AppRegistry.registerComponent('SimpleApp', () => MySimpleApp);
Run Code Online (Sandbox Code Playgroud)

这是它在Portrait中呈现的方式(这是正确的): 肖像

但是,当设备旋转时.红色视图不会相应旋转. 景观

Pab*_*rde 18

最简单的方法是:

import React, { Component } from 'react';
import { Dimensions, View, Text } from 'react-native';

export default class Home extends Component {
  constructor(props) {
    super(props);

    this.state = {
      width: Dimensions.get('window').width,
      height: Dimensions.get('window').height,
    }

    this.onLayout = this.onLayout.bind(this);

  }

  onLayout(e) {
    this.setState({
      width: Dimensions.get('window').width,
      height: Dimensions.get('window').height,
    });
  }

  render() {
    return(
      <View 
        onLayout={this.onLayout}
        style={{width: this.state.width}}
      >
        <Text>Layout width: {this.state.width}</Text>
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我会推荐给大家 (2认同)

Raj*_*shu 14

在本地反应中响应方向变化非常简单.本机中的每个视图都有一个名为onLayout的侦听,它会在方向更改时调用.我们只需要实现这一点.最好将维度存储在状态变量中并更新每个方向更改,以便在更改后重新呈现.另外,我们需要重新加载视图以响应方向更改.

在此输入图像描述

 import React, { Component } from "react";

 import { StyleSheet, Text, View, Image, Dimensions } from "react-native";

 var { height, width } = Dimensions.get("window");

export default class Com extends Component {
constructor() {
    console.log("constructor");
    super();
    this.state = {
        layout: {
            height: height,
            width: width
        }
    };
}
_onLayout = event => {
    console.log(
        "------------------------------------------------" +
            JSON.stringify(event.nativeEvent.layout)
    );

    this.setState({
        layout: {
            height: event.nativeEvent.layout.height,
            width: event.nativeEvent.layout.width
        }
    });
};

render() {
    console.log(JSON.stringify(this.props));
    return (
        <View
            style={{ backgroundColor: "red", flex: 1 }}
            onLayout={this._onLayout}
        >
            <View
                style={{
                    backgroundColor: "green",
                    height: this.state.layout.height - 10,
                    width: this.state.layout.width - 10,
                    margin: 5
                }}
            />
        </View>
    );
}
}
Run Code Online (Sandbox Code Playgroud)

  • 我尝试过这个似乎没有用.应用程序启动时触发了onLayout函数,但旋转模拟器并未重新触发该事件. (8认同)

Tom*_*son 8

对于最新版本的React Native,方向更改不一定会触发onLayout,但会Dimensions提供更直接相关的事件:

class App extends Component {
    constructor() {
        super();
        this.state = {
            width: Dimensions.get('window').width,
            height: Dimensions.get('window').height,
        };
        Dimensions.addEventListener("change", (e) => {
            this.setState(e.window);
        });
    }
    render() {
        return (            
            <View
                style={{
                    width: this.state.width,
                    height: this.state.height,
                }}
            >
            </View>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,此代码用于应用程序的根组件。如果在应用程序中更深入地使用它,则需要包括一个相应的removeEventListener调用。

  • 对于 2020 年 3 月创建的新 RN 项目,我还必须将 `android:screenOrientation="fullSensor"` 添加到 `android/app/src/main/AndroidManifest.xml` 中。 (2认同)

Har*_*hak 5

您可以使用react-native-orientation来检测和执行方向更改。

var Orientation = require('react-native-orientation');
Run Code Online (Sandbox Code Playgroud)

还可以使用Dimension类,该类返回size(width,height)。

Dimensions.get('window')
Run Code Online (Sandbox Code Playgroud)

使用这些方法来适应方向

componentDidMount() {
    Orientation.lockToPortrait(); //this will lock the view to Portrait
    //Orientation.lockToLandscape(); //this will lock the view to Landscape
    //Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations
    // self = this;
    console.log('componentDidMount');
    Orientation.addOrientationListener(this._orientationDidChange);
  }

  componentWillUnmount() {
    console.log('componentWillUnmount');
    Orientation.getOrientation((err,orientation)=> {
        console.log("Current Device Orientation: ", orientation);
    });
    Orientation.removeOrientationListener(this._orientationDidChange);
  }

  _orientationDidChange(orientation) {

    console.log('Orientation changed to '+orientation);
    console.log(self);

     if (orientation == 'LANDSCAPE') {
       //do something with landscape layout
       screenWidth=Dimensions.get('window').width;
       console.log('screenWidth:'+screenWidth);
     } else {
       //do something with portrait layout
       screenWidth=Dimensions.get('window').width;
       console.log('screenWidth:'+screenWidth);

     }

     self.setState({
       screenWidth:screenWidth
     });

   }
Run Code Online (Sandbox Code Playgroud)

我也用了,但是它的性能太低了。

希望有帮助...


Pri*_*ank 0

好的。我找到了这个问题的答案。需要在我们的视图控制器中实现以下内容并在其中调用刷新我们的 ReactNative 视图。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation