onContentSizeChange在Android上没有触发高度变化(react-native)

时间:2017-01-16 06:52:33

标签: react-native react-native-android

onContentSizeChange似乎无法在Android上正常启动。事件在mount上触发,但在text height更改时未触发。相同的代码适用于iOS:

<TextInput
  editable = {true}
  multiline = {true}
  [...]
  onContentSizeChange={(event) => {
  this.setState({height: event.nativeEvent.contentSize.height});
  }}
  style={{height: Math.max(35, this.state.height)}}
/>

2 个答案:

答案 0 :(得分:7)

https://github.com/facebook/react-native/issues/6552#issuecomment-269989962

在Android onContentSizeChange中只调用一次 使用onChange代替onContentSizeChange

<强>代码

<TextInput
  editable = {true}
  multiline = {true}
  [...]
  onChange={(event) => {
  this.setState({height: event.nativeEvent.contentSize.height});
  }}
  style={{height: Math.max(35, this.state.height)}}
/>

答案 1 :(得分:0)

每次更改后设置value为我解决了问题

<TextInput
  multiline={true}
  onChangeText={(text) => {
    this.setState({text});
  }}
  onContentSizeChange={(event) => {
    this.setState({height: event.nativeEvent.contentSize.height});
  }}
  style={{height: this.state.height}}
  value={this.state.text}
/>