KeyboardAvoidingView not Working Properly

前端 未结 14 2110
悲&欢浪女
悲&欢浪女 2020-12-02 08:51

KeyboardAvoidingView not Working Properly

I am trying to use the KeyboardAvoidingView with behavior="padding".

For some reason, when I

相关标签:
14条回答
  • 2020-12-02 09:41

    This is a known issue with KeyboardAvoidingView and Android. There are multiple ways to address this issue.

    React Native documentation says:

    Android may behave better when given no behavior prop at all, whereas iOS is the opposite.

    So, if you are working only with Android you may remove behavior prop and it should work straight away. For best results add android:windowSoftInputMode="adjustResize" to your Manifest.

    Alternatively you can give an offset value that works for you something like this: KeyboardAvoidingView keyboardVerticalOffset={-500} behavior="padding"

    For ios do the same thing conditionally:

    behavior= {(Platform.OS === 'ios')? "padding" : null}

    keyboardVerticalOffset={Platform.select({ios: 0, android: 500})}

    0 讨论(0)
  • 2020-12-02 09:41

    WARNING

    This appears to be only a partial solution, although it works initially, if the android phone is locked on the screen with the keyboard avoiding layout, when you unlock you end up with the extra padding above the keyboard again.

    tl;dr

    Remove android:windowSoftInputMode="adjustResize" from the AndroidManifest.xml

    Before

    ...
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"  
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
      android:windowSoftInputMode="adjustResize"
      >
    ...
    

    After

    ...
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"  
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
      >
    ...
    

    Why

    If I understand the issue correctly, I have been dealing with the same thing. By having android:windowSoftInputMode="adjustResize" in the manifest, the android system will try to do the same job as the KeyboardAvoidingView. This results in extra spacing being added above the keyboard on Android only.

    If working on both platforms you are going to have to deal with this on iOS every time you are working with keyboard input, so best to remove the android specific behaviour by android:windowSoftInputMode="adjustResize" from the manifest and using the KeyboardAvoidingView every time.

    0 讨论(0)
  • 2020-12-02 09:43

    My problem was not checking this platform type

    adding the code below to KeyboardAvoidView fixed it for me

    behavior={Platform.OS === "ios" ? "padding" : 'height'}
    
    0 讨论(0)
  • 2020-12-02 09:44

    The main issue with KeyboardAvoidingView is that; the understanding of how it works is missing.

    I found below link very helpful

    https://medium.com/@nickyang0501/keyboardavoidingview-not-working-properly-c413c0a200d4

    1. First thing, use of flex:1 in KeyboardAvoidingView and behavior: 'padding'
    2. Next is use of flex:1 in "MainView" which needs to go inside KeyboardAvoidingView
    3. Last is adding justifyContent: "flex-end" to ""MainView""

    Hope it helps

    0 讨论(0)
  • 2020-12-02 09:47
     <KeyboardAvoidingView style={styles.keyboardcontainer} behavior="padding"
           keyboardVerticalOffset={Platform.select({ios :120, android : 500})}
           enabled>
    
        <View  style={{flex: 1 }}>
            // Your Code 
        </View>
    
    </KeyboardAvoidingView>
    
    0 讨论(0)
  • 2020-12-02 09:55

    I think the best approach is to create a HOC for this, in addition ,by using getBottomSpace from react-native-iphone-x-helper you can solve overlapping issue for IPhone X and..

     import React, { ComponentType, ReactNode } from 'react';
        import { Platform, KeyboardAvoidingView, View, Pressable, Keyboard } from 
        'react-native';
        import { getBottomSpace } from 'react-native-iphone-x-helper';
    
        interface IProps {
        children: ReactNode;
        }
    
        const KeyboardAvoidingViewHoc = (Component: ComponentType) => {
        return ({ children, ...props }: IProps) => {
        return (
          <KeyboardAvoidingView  {...props} keyboardVerticalOffset= 
       {getBottomSpace()} behavior= {Platform.OS === 'ios' ? 'padding' : undefined}>
            <Pressable onPress={Keyboard.dismiss}>
              <Component {...props}>{children}</Component>
            </Pressable>
          </KeyboardAvoidingView>
        );
      };
    };
    
    export const AvoidKeyboardAvoidingViewHoc = KeyboardAvoidingViewHoc(View);
    
    0 讨论(0)
提交回复
热议问题