I am trying to use the KeyboardAvoidingView with behavior="padding"
.
For some reason, when I
I think this is because the behavior props value, so I think adding this line in the keyboardavoidview will help
<KeyboardAvoidingView
style = {{ flex: 1 }}
behavior={Platform.OS === "ios" ? "padding" : null}>
</KeyboardAvoidingView>
<KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : null}
style={{flex: 1 }}>
In the above snippet, flex is set to 1, which renders the text Input field just above the keyboard by default even when the keyboard isn't opened. And, when the keyboard pops up, the input field is further pushed up by a factor of keyboard height's offset.
Setting flex to 0 shall fix the issue, as it did in my case.
<KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : null}
style={{flex: 0 }}>
<View>
...
</View>
</KeyboardAvoidingView>
If you are using react-navigation, this is affected by the header of the react-navigation. The height of the header is vary on different mobile screen. So you have to get the height of the header and pass into the keyboardVerticalOffset props.
import { Header } from 'react-navigation-stack';
<KeyboardAvoidingView
keyboardVerticalOffset = {Header.HEIGHT + 20} // adjust the value here if you need more padding
style = {{ flex: 1 }}
behavior = "padding" >
<ScrollView>
<TextInput/>
<TextInput/>
<TextInput/>
<TextInput/>
<TextInput/>
<TextInput/>
</ScrollView>
</KeyboardAvoidingView>
<KeyboardAvoidingView styles={styles.container} behavior = 'padding' enabled>
<ScrollView>
<View>
....
</View>
</ScrollView>
</KeyboardAvoidingView>
The KeyboardAvoidingView
must be a ScrollView
child, not the other way around. This way it behaves normal(normal for what purpose I am using it). Try it and let me know how it went.
<ScrollView>
<KeyboardAvoidingView styles={styles.container} behavior='padding'>
</KeyboardAvoidingView>
</ScrollView>
My issue was with the keyboardHidesTabBar
option. The following setup worked for me:
const AppBottomTabNavigator = createBottomTabNavigator(
{
...
},
{
tabBarOptions: {
keyboardHidesTabBar: Platform.OS !== 'ios',
},
},
);
Component:
import React from 'react';
import {
Keyboard,
KeyboardAvoidingView,
Platform,
StyleSheet,
Text,
TextInput,
TouchableWithoutFeedback,
View,
} from 'react-native';
import { Header } from 'react-navigation-stack';
const styles = StyleSheet.create({
container: {
flex: 1,
},
center: {
justifyContent: 'center',
alignItems: 'center',
},
textInput: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
},
});
const MyScreen = () => {
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : null}
keyboardVerticalOffset={Header.HEIGHT}
style={styles.container}
>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.container}>
<View style={[styles.container, styles.center]}>
<Text>Hello!</Text>
</View>
<TextInput style={styles.textInput} placeholder="Message" />
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
);
};
export default MyScreen;