ios - React-Native + Flex 不响应方向变化

标签 ios flexbox react-native

我正在使用 React-Native 编写通用 iPhone/iPad 应用程序。但是,当方向发生变化时,我很难正确地呈现我的 View 。以下是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);

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

但是当设备旋转时。红色 View 不会相应旋转。 Landscape

最佳答案

最简单的方法是:

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>
    );
  }
}

关于ios - React-Native + Flex 不响应方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29914572/

相关文章:

ios - 从 SKScene 呈现后无法关闭 View Controller

html - Flex div继承祖 parent 的高度而不是 parent 的

android - React-native 键盘在文本输入中的 android 三星设备上没有逗号

从 viewController 调用 setDelegate 后 iOS 自定义委托(delegate)返回 Nil

ios - 初始化器不会覆盖其父类(super class)中的指定初始化器

css - 宽度不在 IE11 中换行

html - 使 flex 元素换行以形成 2 列网格

reactjs - 我如何从 laravel api 推送通知来 react native 应用程序?

react-native - 在 react-native 中添加 apple-pay 作为支付方式

ios - RxSwift。依次执行单独的 Observables