Created
May 23, 2019 19:05
-
-
Save 8ui/9fa540d7f5bbb182b9acbc2c2af990fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { Keyboard, TextInput } from 'react-native'; | |
class Example extends Component { | |
state = { | |
keyboardOffset: 0, | |
}; | |
componentDidMount() { | |
this.keyboardDidShowListener = Keyboard.addListener( | |
'keyboardDidShow', | |
this._keyboardDidShow, | |
); | |
this.keyboardDidHideListener = Keyboard.addListener( | |
'keyboardDidHide', | |
this._keyboardDidHide, | |
); | |
} | |
componentWillUnmount() { | |
this.keyboardDidShowListener.remove(); | |
this.keyboardDidHideListener.remove(); | |
} | |
_keyboardDidShow(event) { | |
this.setState({ | |
keyboardOffset: event.endCoordinates.height, | |
}) | |
} | |
_keyboardDidHide() { | |
this.setState({ | |
keyboardOffset: 0, | |
}) | |
} | |
render() { | |
return <View style={{flex: 1}}> | |
<TextInput | |
style={{ | |
position: 'absolute', | |
width: '100%', | |
bottom: this.state.keyboardOffset, | |
}} | |
onSubmitEditing={Keyboard.dismiss} | |
/> | |
</View>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment