react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[KeyboardAvoidingView] Cannot handle several `TextInput`s at the same time? Question

Open Swordsman-Inaction opened this issue 10 years ago • 29 comments

react-native-cli: 0.2.0 react-native: 0.29.2

IOS, on Mac

Use KeyboardAvoidingView according to: Source: https://github.com/facebook/react-native/blob/0.29-stable/Libraries/Components/Keyboard/KeyboardAvoidingView.js Example: https://github.com/facebook/react-native/blob/0.29-stable/Examples/UIExplorer/KeyboardAvoidingViewExample.js

Problem: I have a View which contains several TextInputs, so I added KeyboardAvoidingView on the root of the view, like this:

<KeyboardAvoidingView behavior="height" style={styles.container}>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="L"
        />
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="O"
        />
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="L"
        />
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="LOL"
        />
      </KeyboardAvoidingView>

I'v tried all three behaviors: position, height and padding, they all act strangely: position

position

height

height

padding

padding

Then I tried to just wrap the last TextInput with KeyboardAvoidingView, like this:

      <View style={styles.container}>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="L"
        />
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="O"
        />
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="L"
        />
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
        <KeyboardAvoidingView behavior="position" style={{alignSelf: 'stretch'}}>
          <TextInput
            style={styles.textInput}
            placeholder="lol"
            defaultValue="LOL"
          />
        </KeyboardAvoidingView>
      </View>

when I choose position for behavior, it's like this:

part_position

Seems like every TextInput can trigger the KeyboardAvoidingView's behavior.

Another problem: When KeyboardAvoidingView is wrapped with a position: 'absolute' View, it doesn't work at all, like this:

      <View style={styles.container}>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="L"
        />
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="O"
        />
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <TextInput
          style={styles.textInput}
          placeholder="lol"
          defaultValue="L"
        />
        <View style={{position: 'absolute', top: 470, right: 0, left: 0, bottom: 0}}>
          <Text style={styles.instructions}>
            Press Cmd+R to reload,{'\n'}
            Cmd+D or shake for dev menu
          </Text>
          <KeyboardAvoidingView behavior="position" style={{alignSelf: 'stretch'}} >
            <TextInput
              style={styles.textInput}
              placeholder="lol"
              defaultValue="LOL"
            />
          </KeyboardAvoidingView>
        </View>
      </View>

Behavior:

inside_position

Question: Did I use KeyboardAvoidingView in a wrong way? Or it can not handle multiple TextInputs

Swordsman-Inaction avatar Jul 18 '16 04:07 Swordsman-Inaction

https://github.com/APSL/react-native-keyboard-aware-scroll-view is a great one.

Swordsman-Inaction avatar Jul 22 '16 10:07 Swordsman-Inaction

@nicklockwood KeyboardAvoidindView can handle this scenario?

sibelius avatar Jul 25 '16 15:07 sibelius

And why it's not documented? Is it because it's not production ready?

inetfuture avatar Jul 29 '16 08:07 inetfuture

I'm seeing the same thing - It'd be great to get some comment on whether we can use KeyboardAvoidindView and whether it supports multiple text fields

lprhodes avatar Aug 25 '16 06:08 lprhodes

This issue should not have been closed with a link to a third party solution. The problem persists, and the problem lies within this repository. Using another repository is ultimately not the answer.

qualifyapp avatar Sep 17 '16 05:09 qualifyapp

Reopen it since it still exists. Thanks, @qualifyapp

Swordsman-Inaction avatar Sep 17 '16 13:09 Swordsman-Inaction

Try adjusting the style For example, <KeyboardAvoidingView behavior="position" style={{alignItems: 'center',}}>

jhack32 avatar Sep 20 '16 21:09 jhack32

@Swordsman-Inaction I am encountering the same situation as you did. This problem persists in version 0.39.2 of React Native.

APSL/react-native-keyboard-aware-scroll-view works fine though. Thanks you for sharing it ;)

jrwm avatar Dec 13 '16 13:12 jrwm

Yea it seems like KeyboardAvoidingView isn't working for me at all, and I think @Swordsman-Inaction pointed out why. The views they are in are absolutely positioned. This is (I believe) impossible to avoid when using NavigationExperimental because all the scenes are absolutely positioned to do the animations properly.

keeleycarrigan avatar Dec 16 '16 19:12 keeleycarrigan

This component does not work in any expected way, it surely does some weird stuff;

It would be good to update documentation with multiple inputs/scroll view, if we are not using it right. However I tried (seemingly) all the possible scenarios, also setting contentContainerStyle; just got really tired of it after 3 hrs of no acceptable result...

Maybe you should implement keyboard-aware-scroll approach :D

otoinsa avatar Jan 12 '17 15:01 otoinsa

Is issue solved yet ? I still see it. The keyboard is covering the TextInput.

anahitsk123 avatar Mar 09 '17 13:03 anahitsk123

cmon guys I think this is a very important feature in an mobile app! ESSENTIAL!

buskerone avatar Mar 09 '17 19:03 buskerone

Who solved this important problem? https://github.com/APSL/react-native-keyboard-aware-scroll-view is working well on iOS as expected but it's not working on Android. I've tried everything what I can do including KeyboardAvoidingView and other libraries. But none of them working well on Android. Is there any library which supports Android very well?

binzailani3136 avatar Mar 14 '17 23:03 binzailani3136

KeyboardAvoidingView is very hard to get working. On Android, you don't need it at all thanks to the new android:windowSoftInputMode="adjustResize" setting in the AndroidManifest.xml file. For all of my use cases, this setting works beautifully on Android.

When you have absolute or fixed-size views, KeyboardAvoidingView is simply not working.

drblmb avatar Apr 12 '17 21:04 drblmb

@drblmb thanks, you solve this problem on my android device :D

jdgarzon64 avatar Apr 24 '17 15:04 jdgarzon64

@drblmb it works perfectly.. thanks

jbethuel avatar May 17 '17 02:05 jbethuel

I'm the author of react-native-keyboard-aware-scroll-view. I would be happy to:

  • Integrate my solution into RN core.
  • Help to improve KeyboardAvoidingView with a similar approach than the one I've used.
  • Help to improve KeyboardAvoidingView.
  • Keep maintaining my component as a separated repo.

And I still don't think that linking to a third party solution should close the issue. Keyboard handling should live into the core, IMHO.

alvaromb avatar Jun 09 '17 15:06 alvaromb

I got a good result using android:windowSoftInputMode="adjustResize" for Android and IQKeyboardManager for iOS.

I also created a library for ease of use.

Just install, no extra code needed:

npm install --save react-native-keyboard-manager

douglasjunior avatar Jun 22 '17 19:06 douglasjunior

KeyboardAvoidingView does NOT handle TextInputs at all (separately or not). The whole purpose of KeyboardAvoidingView is reacting on software keyboard appearance events and adjusting own layout accordingly. It can change own position, size, and padding (the most common use case).

KeyboardAvoidingView does NOT change ScrollView padding (aka insets). KeyboardAvoidingView does NOT scroll ScrollViews to focused TextInputs.

So, usually KeyboardAvoidingView should be used as top-level view container.

shergin avatar Jul 18 '17 21:07 shergin

https://github.com/baijunjie/react-native-input-scroll-view

baijunjie avatar Sep 10 '17 08:09 baijunjie