React Native : Subviews of KeyboardAvoidingView have lag / move at different speeds
Solution 1: Quick fix for iOS
You can wrap your elements in a View, which will make them react to keyboard the way you want:
// styles
contentContainer: {
alignItems: 'center',
}
// SearchScreen
<View style={styles.contentContainer}>
<Text style={styles.searchTitle}>Search for a Movie in the OMDB Database</Text>
<TextInput style={styles.searchField} onChangeText={text => this.setState({search:text})} ></TextInput>
<SearchButton navigation = {this.props.navigation} getArguments={getArguments}/>
</View>
However, this will only work on iOS. Keyboard works slightly differently on Android. So solution 2 is a more solid way to do things.
Solution 2: Animations
Keyboard avoidance is quite tricky, Spencer Carli's article that Dominik referred to is a great resource for solutions using KeyboardAvoidingView
. Those usually give you what you need. But if you want a smooth, controlled transition between keyboard states, you should use animations. The flow would go like this:
- Add Keyboard event listeners to your component (
keyboardDidShow
andkeyboardDidHide
) - Wrap the content you want to move in an
Animated.View
- on
keyboardDidShow
, animate the y position of theAnimated.View
by the offset you want. The event returned (of typeKeyboardEvent
), along with theDimensions
API have all the measurements you need. Tip: use anAnimated.timing
animation with bezierEasing
to control how the view moves. - on
keyboardDidHide
, repeat the animation but in the opposite direction.
Hope this helps!