在键盘 appear/hide 时响应本机 enable/disable ScrollView

React Native enable/disable ScrollView when Keyboard appear/hide

我想在键盘隐藏时禁用滚动并在键盘出现时启用。

如有任何帮助,我们将不胜感激。提前谢谢你。

React 本机版本:0.50.3

https://facebook.github.io/react-native/docs/keyboard.html

有键盘显示和隐藏的侦听器。

您可以使用这些函数 keyboardDidshow 和 keyboardDidHide 来启用和禁用 scrollView。

import React, { Component } from 'react';
import { Keyboard, TextInput } from 'react-native';

class Example extends Component {
  componentWillMount () {
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
  }

  state {
    toScroll: false
  }

  componentWillUnmount () {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
  }

  _keyboardDidShow () {
    this.setState({ toScroll: true });
  }

  _keyboardDidHide () {
    this.setState({ toScroll: false });
  }

  render() {
    const { toScroll } = this.state;
    return (
      <ScrollView scrollEnabled={toScroll}>
        <View />
      </ScrollView>
    );
  }
}