KeyboardAvoidingView not Working Properly

前端 未结 14 2109
悲&欢浪女
悲&欢浪女 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:31

    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>
    
    0 讨论(0)
  • 2020-12-02 09:33
    <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>
    
    0 讨论(0)
  • 2020-12-02 09:34

    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>
    
    0 讨论(0)
  • 2020-12-02 09:35
    <KeyboardAvoidingView styles={styles.container} behavior = 'padding'  enabled>
       <ScrollView>
    
            <View>
              ....
            </View>
    
       </ScrollView>
    </KeyboardAvoidingView>
    
    0 讨论(0)
  • 2020-12-02 09:37

    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>
    
    0 讨论(0)
  • 2020-12-02 09:38

    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;
    
    0 讨论(0)
提交回复
热议问题