1. 程式人生 > >react native獲取鍵盤高度

react native獲取鍵盤高度

elf ide com nat reac mat height one rem

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

componentWillMount() {
this.keyboardDidShowListener = Keyboard.addListener(‘keyboardDidShow‘, this._keyboardDidShow.bind(this));
this.keyboardDidHideListener = Keyboard.addListener(‘keyboardDidHide‘, this._keyboardDidHide.bind(this));
}

_keyboardDidShow(e) {
async function run(self) {
await self.setState({
keyboardHeight: e.endCoordinates.height
});
self.refs.scroll.scrollTo({
x: 0, y: self.state.keyboardHeight, animated: true
})
}

run(this);
}

_keyboardDidHide(e) {
this.setState({
keyboardHeight: 0
})
}

react native獲取鍵盤高度