React Native 键盘挡住输入框解决方法

一、使用ref 选中外层的ScrollView标签React Native 键盘挡住输入框解决方法_第1张图片

二、在componentDidMount方法中对键盘进行监听
在这里插入图片描述

 // 监听键盘
        this.keyboardWillShowListener = Keyboard.addListener('keyboardWillShow', this.keyboardDidShow);
        this.keyboardWillHideListener = Keyboard.addListener('keyboardWillHide', this.keyboardDidHide);

三、监听方法
React Native 键盘挡住输入框解决方法_第2张图片

 //键盘弹起后执行
    keyboardDidShow = (e) => {
        console.log(e)
        this._scrollView.scrollTo({x: 0, y: e.startCoordinates.height, animated: true});
    }

    //键盘收起后执行
    keyboardDidHide = () => {
        this._scrollView.scrollTo({x: 0, y: 0, animated: true});
    }

四、退出时注销监听
React Native 键盘挡住输入框解决方法_第3张图片

 this.keyboardWillShowListener && this.keyboardWillShowListener.remove();
 this.keyboardWillHideListener && this.keyboardWillHideListener.remove();

你可能感兴趣的:(React,Native)