Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: How to keep multi-line textinput visible above keyboard

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

like image 577
S Kraft Avatar asked May 24 '16 14:05

S Kraft


People also ask

How do you dismiss the keyboard in a multiline TextInput react native?

TextInput has a blurOnSubmit prop; when set to true, the return key dismisses the keyboard. For Multiline TextInput, return key enters a new line and not dismiss the keyboard. So, clicking on some other part of the View should dismiss the keyboard.

How do you move a view up when keyboard appears react native?

How do you control what keyboard pushes up React Native? Use android:windowSoftInputMode="adjustResize". KeyboardAvoidingView and other keyboard-related components don't work quite well if you have "adjustPan" set for your android:windowSoftInputMode in AndroidManifest.


2 Answers

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!

like image 57
Marco Gaspari Avatar answered Oct 25 '22 09:10

Marco Gaspari


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.

like image 32
Daniel Schmidt Avatar answered Oct 25 '22 09:10

Daniel Schmidt