There is a "behavior" property in KeyboardAvoidingView, e.g.:
import { KeyboardAvoidingView } from 'react-native';
<KeyboardAvoidingView style={styles.container} behavior="padding" enabled>
... your UI ...
</KeyboardAvoidingView>
It can be picked as one of three choices: 'height'
, 'position'
, or 'padding'
. The difference is not explained in the documentation. All it says is that it's not required to set the property, and has a note:
Note: Android and iOS both interact with this prop differently. Android may behave better when given no behavior prop at all, whereas iOS is the opposite.
What effect are these settings supposed to have?
KeyboardAvoidingView is a React Native built-in component with full JS implementation. It relies on RN's keyboard events ( keyboardWillChangeFrame on iOS & keyboardDidHide/Show on Android) and, based on provided behavior prop, applies additional padding, translation, or changes container's height.
It is a component to solve the common problem of views that need to move out of the way of the virtual keyboard. It can automatically adjust either its height, position, or bottom padding based on the keyboard height.
Use android:windowSoftInputMode="adjustResize". KeyboardAvoidingView and other keyboard-related components don't work quite well if you have "adjustPan" set for your android:windowSoftInputMode in AndroidManifest. xml . Instead, you should use "adjustResize" and have a wonderful life.
android:windowSoftInputMode="adjustPan" will make the Main activity view PAN move (i.e. be translated) outside of the screen! Use "adjustResize" if you want your components to resize (e.g. put one <View> with style={{flex:1}} and it will be the one that resizes when the keyboard is visible).
I agree that the lack of documentation here is frustrating. If you dig into the source code for KeyboardAvoidingView
, you will find a switch around the behavior
: https://github.com/facebook/react-native/blob/92073d4a71d50a1ed80cf9cb063a6144fcc8cf19/Libraries/Components/Keyboard/KeyboardAvoidingView.js#L157
It looks like the following is happening:
height
A <View>
is returned with styling that attempts to set a static height to the view that is either the screen height, or the screen height minus the keyboard when the keyboard is present.
position
A nested <View>
is returned, where the outer View has your styles applied, and the inner View has a bottom
style applied that attempts to match the keyboard height when the keyboard is present.
padding
A single <View>
is returned where a paddingBottom
style is applied to the height of the keyboard if the keyboard is present.
Given the arbitrary options available, it looks like when using the KeyboardAvoidingView
you should exercise trial and error, and check both devices for your desired outcome. In theory all three options should work, but as the docs say there is some nuance between device types.
In my opinion, this component should be scrapped though, in favour of helper functions that would return keyboard heights over time, so you could apply your own style ideas directly based on keyboard visibility.
Allow me to go through these attributes for the prop behavior one by one.
I am considering the <TextInput>
object for which our keyboard gets called.
**Note: when we are wrapping ou screen inside a ScrollView make sure to wrap the ScrollView with <KeyboardAvoidingView style={{flex:1}} ...> where flex:1 should be used to occupy the entire component as ScrollView is wrapping the Component **
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With