Hey ๐
Update: 8th April 2020
If you are using React navigation V5 then use
import { HeaderHeightContext } from "@react-navigation/stack";
<HeaderHeightContext.Consumer>
{headerHeight => (
<KeyboardAvoidingView
{...(Platform.OS === "ios" ? { behavior: "padding" } : {})}
// you might need sometimes๐
contentContainerStyle={{flex: 1}}
// chances are you might be using react-navigation
// if so ๐
keyboardVerticalOffset={headerHeight + 64}
// You can import Header Component from react-navigation and it has height attached to it
// 64 is some extra padding, I feel good, feel free to tweak it
>
{children}
</KeyboardAvoidingView>
)}
</HeaderHeightContext.Consumer>
End of update
Here's how I use KeyboardAvoidingView, this will be the root component for the screen
<KeyboardAvoidingView
{...(Platform.OS === "ios" ? { behavior: "padding" } : {})}
// you might need sometimes๐
contentContainerStyle={{flex: 1}}
// chances are you might be using react-navigation
// if so ๐
keyboardVerticalOffset={Header.HEIGHT + 64}
// You can import Header Component from react-navigation and it has height attached to it
// 64 is some extra padding, I feel good, feel free to tweak it
>
{children}
</KeyboardAvoidingView>
If you are still facing problems, try to understand where KeyboardAvoidingView Component is from top of the screen and then adjust keyboardVerticalOffset based on that. That's the key to fix it
Top comments (4)
instead of
{...(Platform.OS === "ios" ? { behavior: "padding" } : {})}
I recommend
behavior={Platform.select({ ios: 'padding' })}
this will also evaluate to
undefined
on Android.If you're using React Navigation 5 you can use useHeaderHeight hook from @react-navigation/stack.
Yes, thanks for the suggestion
Guys Check this Example,
Its 100% solution for Android and Ios.
github.com/guptavishesh143/React-N...
If you need help let me know!
Thank You