How to avoid keyboard pushing layout up on Android react-native

浪子不回头ぞ 提交于 2020-05-09 20:20:10

问题


I'm facing this issue for a few months and I still can't solve it. I have a text input at the bottom of the view that is supposed to rise up with the soft keyboard once tapped. Instead, the whole layout is pushed up and you can't see the upper part of it.

I tried many different keyboard spacer libraries but they all just push the TextInput even higher..

Screenshots: Without keyboard With keyboard

Here is my main View:

<View
    style={{
      flex: 1,
      alignItems: 'stretch',
      justifyContent: 'space-between',
      overflow: 'hidden',
      backgroundColor: Colors.darkBlue
    }}
  >
    {/* Header */}
    <View
      style={{
        flexDirection: 'row',
        alignItems: 'stretch',
        height: 300
      }}>
      {/* Question bubble */}
      { (this.state.question && this.state.question !== '') ? (
          <TouchableOpacity
            style={{
                flex: 1,
                flexDirection: 'row',
                backgroundColor: 'transparent',
                alignItems: 'stretch',
                paddingRight: QUESTION_SPEAKER_RADIUS
              }}
          >
            <View
              style={{
                  flex: 1,
                  alignSelf: 'stretch',
                  alignItems: 'center',
                  justifyContent: 'center',
                  backgroundColor: 'white',
                }}
            >
              <Text>
                {this.state.question}
              </Text>
            </View>
          </TouchableOpacity>
        ) : null
      }
    </View>
    <KeyboardInput
      style={{}}
      onClose={() => this.setState({ displayMode: DISPLAY_MODES.homeEmpty })}
      onConfirm={(text) => this.onConfirmText(text) }
    />
  </View>

Here is KeyboardInput:

<View
      style={{
        alignSelf: 'stretch',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'flex-end',
        backgroundColor: Colors.pink,
        borderColor: Colors.lime,
        borderTopWidth: 4,
        padding: 6,
      }}>
      <View
        style={{
          flex: 1,
          borderRadius: 6,
          padding: 0,
          backgroundColor: Colors.white,
          alignItems: 'stretch',
        }}
      >
        <TextInput
          placeholder={Strings.child_keyboard_placeholder}
          value={this.state.messageTextInput}
          onChangeText={(text) => this.setState({messageTextInput: text})}
          style={{
            height: 50,
            marginLeft: 10,
            marginRight: CONFIRM_BUTTON_SIZE / 2
          }}
          underlineColorAndroid='transparent'
          numberOfLines={2}
          maxLength={70}
          autoCorrect={false}
          returnKeyType='next'
        />
      </View>
    </View>

Using RN 0.35 on Android.


回答1:


The problem here is that you have in your AndroidManifest.xml:

windowSoftInputMode="adjustResize";

Change it to:

windowSoftInputMode="adjustPan"

Note: after this change you'll need run ./gradlew clean in android folder and react-native run-android in your project directory




回答2:


putting

android:windowSoftInputMode="adjustNothing"

in Manifest worked for me.




回答3:


I tried all the solutions I could find in GitHub or stack overflow. Adding the following line of code in AndroidManifest.xml will be helpful.

I've tried this

windowSoftInputMode="adjustPan"

Sometimes it works but it misbehaves.

And then I came accross this

windowSoftInputMode="adjustResize"

This also misbehaves I finally combined both of them something like this and it works fine out of the box.

android:windowSoftInputMode="adjustPan|adjustResize"

And then you can use react-native KeyboardAvoidingView component.

<KeyboardAvoidingView
    style={{ flex: 1}}
    behavior={Platform.OS === 'ios' ? 'padding' : undefined}
    keyboardVerticalOffset={Platform.OS === 'ios' ? 40 : 0}
  >

Hope this helps you.




回答4:


Well, thanks to the React Native FB group I got a solution: The status bar has to be not 'hidden' in order for this to work. Really weird bug..




回答5:


Here are a couple deadly tutorials for anyone venturing into this area:

  • https://medium.freecodecamp.org/how-to-make-your-react-native-app-respond-gracefully-when-the-keyboard-pops-up-7442c1535580

  • https://medium.com/@bosung90/use-higher-order-component-in-react-native-df44e634e860

I will look very closely at the HOC solution myself shortly for this project, but for now, I just need this problem gone, which windowSoftInputMode="adjustPan" did for me.

With that, the keyboard just comes up over top of the view. With the default resize option, every component was using flex: 1 and justify/align center so the view got all kinds of mangled. Considering my view only has one input and is part of an 8 view, 8 step process, this is way too much work for me given the keyboard doesn't even go over top of the input with adjustPan.

But, in my opinion, the optimal solution is to use <KeyboardAvoidingView /> in conjunction with add and remove event listeners and swapping component styles with some state toggles. It's the only way to get full control over the UI on both Android and iOS through both keyboard states.




回答6:


The only solution that worked for me is:

Define keyboardVerticalOffset and put everything in a ScrollView put keyboardVerticalOffset={-500}

<KeyboardAvoidingView style = {styles.container} 
behavior="position" keyboardVerticalOffset={-550}>  

ScrollView
    TextInput

I tried many solutions from changing android windowsoft in XML to height of container and what not then I found this solution on Github.




回答7:


Setting

android:windowSoftInputMode="adjustPan"

on the MainActivity in AndroidManifest.xml worked for me !

However android:windowSoftInputMode="adjustNothing" is not suggested as it completely takes your freedom of making any design responsive to keyboard.




回答8:


I had a particular situation where the only way I could prevent this from happening was by giving the problematic TextInput a minHeight in its style.



来源:https://stackoverflow.com/questions/42840555/how-to-avoid-keyboard-pushing-layout-up-on-android-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!