KeyboardAvoidingView
This component will automatically adjust its height, position, or bottom padding based on the keyboard height to remain visible while the virtual keyboard is displayed.
Why another KeyboardAvoidingView is needed?β
This new KeyboardAvoidingView maintains the familiar React Native API but ensures consistent behavior and animations on both iOS and Android platforms. Unlike the existing solution, which primarily caters to iOS, this component eliminates platform discrepancies, providing a unified user experience. By reproducing the same animations and behaviors on both platforms, it simplifies cross-platform development, meets user expectations for consistency, and enhances code maintainability. Ultimately, it addresses the need for a reliable and uniform keyboard interaction solution across different devices.
Below is a visual difference between the implementations (the animation is 4x times slower for better visual perception).
Default react-native implementation on Android | Implementation from react-native-keyboard-controller with better animations |
If you found any bugs or inconsistent behavior comparing to react-native implementation - don't hesitate to open an issue. It will help the project π
Also if there is any well-known problems in original react-native implementation which can not be fixed for a long time and they are present in this implementation as well - also feel free to submit an issue. Let's make this world better together π
Exampleβ
import React from "react";
import {
Text,
TextInput,
TouchableOpacity,
View,
StyleSheet,
} from "react-native";
import { KeyboardAvoidingView } from "react-native-keyboard-controller";
export default function KeyboardAvoidingViewExample() {
return (
<KeyboardAvoidingView
behavior={"padding"}
keyboardVerticalOffset={100}
style={styles.content}
>
<View style={styles.inner}>
<Text style={styles.heading}>Header</Text>
<View>
<TextInput placeholder="Username" style={styles.textInput} />
<TextInput placeholder="Password" style={styles.textInput} />
</View>
<TouchableOpacity style={styles.button}>
<Text style={styles.text}>Submit</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
content: {
flex: 1,
maxHeight: 600,
},
heading: {
fontSize: 36,
marginBottom: 48,
fontWeight: "600",
},
inner: {
padding: 24,
flex: 1,
justifyContent: "space-between",
},
textInput: {
height: 45,
borderColor: "#000000",
borderWidth: 1,
borderRadius: 10,
marginBottom: 36,
paddingLeft: 10,
},
button: {
marginTop: 12,
height: 45,
borderRadius: 10,
backgroundColor: "rgb(40, 64, 147)",
justifyContent: "center",
alignItems: "center",
},
text: {
fontWeight: "500",
fontSize: 16,
color: "white",
},
});
Propsβ
View Propsβ
Inherits View Props.
behaviorβ
Specify how to react to the presence of the keyboard. Could be one value of:
-
translate-with-padding- Combines translation (moves the view up) with paddingTop that gets applied only once per animation. As a result this mode delivers the best possible performance. An ideal fit for building chat-like apps. -
padding- the most commonly used mode. It moves the view up by the height of the keyboard and appliespaddingBottomto the content container. Usepaddingmode when you have aScrollVieworflex-basedlayouts that should remain visible. -
height- use this when you want to shrink the entire view rather than moving specific elements. -
position- use it when the view needs to shift up instead of resizing. For example when you have a fixed button at the bottom (also be sure that you checked out KeyboardStickyView component).
contentContainerStyleβ
The style of the content container (View) when behavior is position.
enabledβ
A boolean prop indicating whether KeyboardAvoidingView is enabled or disabled. Default is true.
keyboardVerticalOffsetβ
This is the distance between the top of the user screen and the react native view. This is particularly useful when there are fixed headers, navigation bars, or other UI elements at the top of the screen. Default is 0.
When to use keyboardVerticalOffset?
You should use keyboardVerticalOffset in the following scenarios:
- Navigation Bars / Headers - If your screen is inside a
Stack.Navigatorfromreact-navigation, the header height should be compensated usingkeyboardVerticalOffset:
import { useHeaderHeight } from "@react-navigation/elements";
import { KeyboardAvoidingView } from "react-native-keyboard-controller";
const MyScreen = () => {
const headerHeight = useHeaderHeight();
return (
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={headerHeight}
>
<TextInput placeholder="Type here..." />
</KeyboardAvoidingView>
);
};
-
Custom Toolbars or Fixed Elements at the Top - If your app has a fixed toolbar, status bar, or other UI elements at the top, you should offset accordingly.
-
Modal Screens with Different Layouts - When using
KeyboardAvoidingViewinside aModal, you may need to manually define the vertical offset to account for the modalβs positioning.
Below shown a visual representation of keyboardVerticalOffset:
StatusBar height on Android with useHeaderHeightOn Android, how you handle the StatusBar height depends on whether the StatusBar is translucent or not:
- If the
StatusBaris translucent,react-navigationautomatically includes theStatusBarheight inuseHeaderHeight(), along with safe-area padding. This behavior aligns with iOS, so you donβt need to manually add theStatusBarheight. - If the StatusBar is not translucent,
useHeaderHeight()does not include theStatusBarheight. In this case, you need to add it manually:
const headerHeight = useHeaderHeight() + (StatusBar.currentHeight ?? 0);
Since StatusBar.currentHeight is an Android-only property, using ?? 0 ensures it doesnβt cause issues on iOS. This approach avoids the need for Platform.OS or Platform.select checks.