Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-slide the window out from behind keyboard when TextInput has focus?

I've seen this hack for native apps to auto scroll the window, but wondering best way to do it in React Native... When a <TextInput> field gets focus and is positioned low in the view, the keyboard will cover up the text field.

You can see this issue in example UIExplorer's TextInputExample.js view.

Does anyone have a good solution?

like image 594
McG Avatar asked Mar 28 '15 03:03

McG


People also ask

How do you auto scroll the screen up when virtual keyboard appears in react native?

You can use ScrollView to control screen up and down movements. As long as user hasn't focused any TextInput , you can disable scroll. On focus, just shift up the scrollview using Content Offset prop.

How does a keyboard avoiding view work?

KeyboardAvoidingView is a React Native built-in component with full JS implementation. It relies on RN's keyboard events ( keyboardWillChangeFrame on iOS & keyboardDidHide/Show on Android) and, based on provided behavior prop, applies additional padding, translation, or changes container's height.

How do I scroll TextInput react native?

Making the text input to scroll in react native, you have to add numberOflines to it, this is the property which takes the value as int, how much lines you want to display in text input you have to provide the number and after that, the text input will get scroll property automatically.


2 Answers

2017 Answer

The KeyboardAvoidingView is probably the best way to go now. Check out the docs here. It is really simple compared to Keyboard module which gives Developer more control to perform animations. Spencer Carli demonstrated all the possible ways on his medium blog.

2015 Answer

The correct way to do this in react-native does not require external libraries, takes advantage of native code, and includes animations.

First define a function that will handle the onFocus event for each TextInput (or any other component you would like to scroll to):

// Scroll a component into view. Just pass the component ref string. inputFocused (refName) {   setTimeout(() => {     let scrollResponder = this.refs.scrollView.getScrollResponder();     scrollResponder.scrollResponderScrollNativeHandleToKeyboard(       React.findNodeHandle(this.refs[refName]),       110, //additionalOffset       true     );   }, 50); } 

Then, in your render function:

render () {   return (     <ScrollView ref='scrollView'>         <TextInput ref='username'                     onFocus={this.inputFocused.bind(this, 'username')}     </ScrollView>   ) } 

This uses the RCTDeviceEventEmitter for keyboard events and sizing, measures the position of the component using RCTUIManager.measureLayout, and calculates the exact scroll movement required in scrollResponderInputMeasureAndScrollToKeyboard.

You may want to play around with the additionalOffset parameter, to fit the needs of your specific UI design.

like image 183
Sherlock Avatar answered Oct 08 '22 01:10

Sherlock


Facebook open sourced KeyboardAvoidingView in react native 0.29 to solve this problem. Documentation and usage example can be found here.

like image 32
farwayer Avatar answered Oct 08 '22 02:10

farwayer