KeyboardAvoidingView与ScrollView

use*_*104 27 reactjs react-native

我是一个新的反应本机,有一个问题,我没有找到反应本机文档.

我正在研究这个组件KeyboardAvoidingView:https: //facebook.github.io/react-native/docs/keyboardavoidingview.html

问题很简单 - 有没有人可以KeyboardAvoidingView很好地与之合作ScrollView?我有很多TextInputs在一个组件(TextInputs高度的总和大于设备高度),一旦键盘出现,我有各种问题..
如果我使用View而不是ScrollView一切都很好,但我不能使用它,因为我需要比设备高度更多的空间..我在KeyboardAvoidingView文档中找不到任何关于Scroll的内容.

谢谢.

Abh*_*urk 20

对于那些人,这就是我的解决方案,它将可以工作并自动在焦点输入上滚动。我在博览会上尝试过此方法,希望对您有所帮助。

<KeyboardAvoidingView style={{ flex: 1, flexDirection: 'column',justifyContent: 'center',}} behavior="padding" enabled   keyboardVerticalOffset={100}>
    <ScrollView>
        <View style={Styles.row}>
                //your view
        </View>
    </ScrollView>
</KeyboardAvoidingView>
Run Code Online (Sandbox Code Playgroud)

  • 效果棒极了,谢谢。不知道为什么这不是正确答案……他甚至没有选择一个。 (3认同)
  • 在 apk 构建中为我工作,但在 expo 构建中不起作用,不用担心哈哈:D (2认同)

Sla*_*nko 20

<View style={{flex:1}}>
   <KeyboardAvoidingView
     style={{flex:1}}
     behavior={Platform.OS === 'ios' ? 'position' : null}
     keyboardVerticalOffset={Platform.OS === 'ios' ? 50 : 70}
   >
    <ScrollView> // no need to put here style={{flex:1}}
      <TextInput />
      <TextInput />
      <TextInput />
      <TextInput />
    </ScrollView>
  </KeyboardAvoidingView>
</View>
Run Code Online (Sandbox Code Playgroud)

  • 这应该被标记为正确答案。谢谢 ;-) (3认同)

use*_*104 13

看起来facebook尚未实现scrollViews的解决方案.但是我找到了Wix所做的解决方案,它的工作原理应该:)

  • 请注意,Wix 的“react-native-keyboard-aware-scrollview”和 APSL 的“react-native-keyboard-aware-scroll-view”之间存在差异。注意到那里多余的破折号了吗?一定会爱上 npm... (8认同)

Was*_*m05 8

我也试图在互联网上找到解决方案,但我自己想出来了.我能够做keyboardAvoidingView与工作ScrollView在iPhone模拟器SE.

我使用了padding类型position,keyboardVerticalOffset设置为更高的值.我希望这能帮助所有陷入困境的人.

 render() {
    return (
    <View style={…..}>
        <ScrollView>
                <KeyboardAvoidingView style={{ flex: 1 }}
                keyboardVerticalOffset={100} behavior={"position"}>
                <TextInput style={styles.editInput} …./>

            </KeyboardAvoidingView>
        </ScrollView>
    </View>
)} 
Run Code Online (Sandbox Code Playgroud)

  • 这是唯一对我有用的答案。谢谢! (3认同)

Flo*_*bre 8

就我而言,我使用了:https : //github.com/APSL/react-native-keyboard-aware-scroll-view

<KeyboardAwareScrollView>
  ....
  <MyContainerComponent>
    ....
    <MyFormComponentWithInputs />
  </MyContainerComponent>
</KeyboardAwareScrollView>
Run Code Online (Sandbox Code Playgroud)

它也支持较旧的 RN 版本。

我的文本输入隐藏得很深,是 ScrollView 的一些自定义子组件,但该包对 Android 和 iOS 都很好