14

I have a TextInput with multiline set to true. I am scrolling to the input on focus with:

scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
  React.findNodeHandle(this.refs.myInput),
  0,
  true
);

However when the multiline TextInput expands the text will be hidden beneath the keyboard. I only want to scroll down when the cursor/current text is not visible. So I can't just run the code above on text change as it would scroll the view even if the current cursor/current text is visible (like editing on the first line).

Is there any way to get the cursor/current text position on screen? Or is there any other way to do what I'm trying to?

Currently:

enter image description here

What I'm trying to achieve: enter image description here

S Kraft
  • 420
  • 4
  • 13

3 Answers3

3

You may use the onLayout method, provided by the View (which Text extends). It returns the dimensions of the view, which you may use to recalculate the scroll position of your scrollview.

Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
1

The best way to do it seems to be with the react-native-keyboard-aware-scroll-view library and use the method _scrollToInput and follow what the docs say.

enter image description here

The one thing I had to change was to call "this.scroll.props.scrollToFocusedInput(reactNode)" instead of "this.scroll.scrollToFocusedInput(reactNode)" in the docs!

Marco Gaspari
  • 479
  • 5
  • 4
0

I found this component... but it's not working for me (I get errors on "import"). I assume that they will get fixed eventually.

https://github.com/VansonLeung/react-native-keyboard-aware-view

elcuco
  • 8,948
  • 9
  • 47
  • 69