2015-05-30 67 views
11

我有一个使用react-native创建的登录屏幕。React-native如何在文本输入上移动屏幕

如何在用户输入textInput时将屏幕向上移动?

我是否在听onFocus()事件并使用css样式来更改视图的样式?

+0

这里我们需要更多的上下文/代码。 –

+0

可能的重复[如何自动从窗口后面的TextInput有焦点时滑出窗口?](http://stackoverflow.com/questions/29313244/how-to-auto-slide-the-window-out-from -behind-keyboard-when-textinput-have-focus) – Sherlock

+0

你可能想要[this](https://github.com/facebook/react-native/issues/3195#issuecomment-146568644)Github问题。我们正在讨论如何在此实现这一目标。 – amb

回答

6

您可以使用ScrollView来控制屏幕的上下移动。只要用户没有关注任何TextInput,您可以disable scroll。关注焦点时,只需使用Content Offset支撑向上移动滚动视图即可。

<TextInput 
    onFocus={this.textInputFocused.bind(this)} 
    /> 

textInputFocused() { 
//do your stuff here. scroll screen up 
} 

希望它有帮助!

+1

在Android上不起作用 – antoine129

3

夜煞的答案是相当不错的,虽然不会与滚动型的contentOffset,我会用ScrollResponder大惊小怪:

render() { 
    return (
    <ScrollView ref="myScrollView"> 
     <TextInput 
     ref="myInput" 
     onFocus={this._scrollToInput.bind(this)} 
     /> 
    </ScrollView> 
); 
} 

_scrollToInput { 
    const scrollResponder = this.refs.myScrollView.getScrollResponder(); 
    const inputHandle = React.findNodeHandle(this.refs.myInput) 

    scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
    inputHandle, // The TextInput node handle 
    0, // The scroll view's bottom "contentInset" (default 0) 
    true // Prevent negative scrolling 
); 
} 

请参阅方法定义:scrollResponderScrollNativeHandleToKeyboard

+0

这将在getScrollResponder上破灭我猜这是在旧版本的api上工作的。我使用scrollTo和硬编码数字它是一个可怕的解决方案,我一半试图使用webviews的表单页面。 – aintnorest

+0

将屏幕向上移动可以正常工作,完成键入后应该如何使屏幕再次向下移动? –

0

而另一种解决方案,工作与RN 0.2,这次,而不是挤压它滚动的内容。

inputFocused: function(ref) { 
    this._scroll(ref, 75); 
}, 

inputBlurred: function(ref) { 
    this._scroll(ref, 0); 
}, 

_scroll: function(ref, offset) { 
    setTimeout(() => { 
    var scrollResponder = this.refs.myScrollView.getScrollResponder(); 
    scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
       React.findNodeHandle(this.refs[ref]), 
       offset, 
       true 
      ); 
    }); 
    }, 

...

render: function() { 
    return <View style={{flex: 1}}> 
    <ScrollView ref="myScrollView" keyboardDismissMode='interactive' contentContainerStyle={{flex: 1}}> 
     <TextInput 
     ref="myInput" 
     onFocus={this.inputFocused.bind(this, 'myInput')} 
     onBlur={this.inputBlurred.bind(this, 'myInput')} /> 
    </ScrollView> 
    </View> 
} 
0

package做了马丽娟的工作,介绍了滚动视图了输入与键盘相匹配,然后滚动回下来KeyboardAwareScrollView组件。

5

在2017年(RN 0.43)有特殊的组件此:KeyboardAvoidingView

+0

不适用于android – farmcommand2

+0

@ farmcommand2它的工作原理,但有一些黑客。在GitHub上有[issue](https://github.com/facebook/react-native/issues/11681#issuecomment-339446150)解决方案。我使用这个:'behavior = {(Platform.OS ==='ios')? 'padding':null}' –

+0

有点蹩脚。我宁愿自己实现键盘事件。所以你真的可以控制布局的行为 –

0

这是一个废话拍摄得到的滚动型工作的原生键盘意识的功能。对于我的Android应用程序来说,它完美地工作在一个与另一个不起作用的屏幕几乎相同的屏幕上。而在iOS上,它不起作用。这是什么工作对我来说:

import { Keyboard, ScrollView, StyleSheet, View } from 'react-native'; 

this.state = { 
    filler: false, 
} 

componentWillMount() { 
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this)); 
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this)); 
} 

componentWillUnmount() { 
    this.keyboardDidShowListener.remove(); 
    this.keyboardDidHideListener.remove(); 
} 

_keyboardDidShow() { 
    this.setState({filler: true}) 
    setTimeout(() => this.vertical && this.vertical.scrollToEnd({animated: true}), 0); 
} 

_keyboardDidHide() { 
    this.setState({filler: false}) 
} 


... 
return (
    <ScrollView ref={ref => this.vertical = ref}> 
    <TextInput/> 
    { this.state.filler ? <View style={styles.filler}/> : null } 
    </ScrollView> 
) 

styles.filler = { 
    height: 'Keyboard Height' 
} 

注意:如果您的<TextInput/>是它是在我的情况下,屏幕的底部,这可能只是工作。

+1

使用箭头函数从es6避免绑定方法https://stackoverflow.com/questions/32192682/react-js-es6-avoid-binding-this-to-every方法 你也可以通过在iOS上执行'keyboardWillShow'来改进它。 (未在Android上实现) –

+0

优秀的建议。我注意到键盘上方出现的TextInput的响应框架稍微落后了。谢谢! – Progoogler