首页 > 解决方案 > 如何防止自动增长文本输入容器在 react-native 中在键盘后面增长

问题描述

目标:创建一个文本输入字段,该字段会随着输入行的扩展而响应扩展,直到某个点,就像大多数消息传递应用程序一样。

问题:当输入扩展超过 3 或 4 行时,输入容器的顶部不是向上扩展,而是根据内容大小扩展,但容器被重新定位在键盘/键盘单词建议后面,这将按钮重新定位在左侧(输入容器的一部分)以及其<TextInput>本身。

下图显示了我希望填充如何位于底部。“下部容器”体现了按钮和输入字段。灰白色是键盘建议的顶部(iOS)

在此处输入图像描述

下图显示了不良行为。您可以看到输入的底部边框,以及按钮开始消失在建议框/键盘后面。如果输入不断扩大,底部边框和按钮将逐渐变得不那么明显。

在此处输入图像描述

这是整个屏幕组件:

render () {
    return (
        <KeyboardAvoidingView
            style={{ flex: 1, backgroundColor: Colors.PRIMARY_OFF_WHITE,
            }}
            behavior={Platform.OS === 'ios' ? 'position' : null}
            keyboardVerticalOffset={Platform.OS === 'ios' ?hp("11%") : 0}
        >

        /* Upper Container */
        <View style={{ justifyContent: 'flex-start', height: height*0.8, paddingBottom: 10 }}>
            <FlatList
                data={this.props.messages}
                ref={'list'}
                onContentSizeChange={() => this.refs.list.scrollToEnd()}
                keyExtractor={(item) => {
                    return (
                        item.toString() +
                        new Date().getTime().toString() +
                        Math.floor(Math.random() * Math.floor(new Date().getTime())).toString()
                    );
                }}
                renderItem={({ item, index }) => {
                    return (
                        <Components.ChatMessage
                            isSender={item.sender == this.props.uid ? true : false}
                            message={item.content.data}
                            read={item.read}
                            time={item.time}
                            onPress={() =>
                                this.props.readMsg(
                                    {
                                        id: this.props.uid,
                                        role: this.props.role,
                                        convoId: this.state.ownerConvoId
                                    },
                                    item.docId,
                                    this.props.navigation.state.params.uid
                                )}
                         />
                      );
                  }}                    
                />
                </View>

            /* Lower Container */
                <View
                    style={{
                        height: this.state.height+20,
                        flexDirection: 'row',
                        alignItems: 'center',
                        justifyContent:"space-around",
                        paddingVertical: 10
                    }}
                >
                    <Components.OptionFan
                        options={[
                            {
                                icon: require('../assets/img/white-voice.png'),
                                onPress: () => {
                                  this.props.navigation.navigate('Schedule', {
                                        receiver: this.conversants.receiver,
                                        sender: this.conversants.sender,
                                        type: 'Voice'
                                    });
                                },
                                key: 1
                             },
                             {
                                icon: require('../assets/img/white-video.png'),
                                onPress: () => {
                                    this.props.navigation.navigate('Schedule', {
                                        receiver: this.conversants.receiver,
                                        sender: this.conversants.sender,
                                        type: 'Video'
                                    });
                                },
                                key: 2
                              }
                          ]}
                      />


                        <TextInput
                            multiline={true}
                            textAlignVertical={'center'}
                            onChangeText={(text) => this.setState({ text })}
                            onFocus={() => this.refs.list.scrollToEnd()}
                            onContentSizeChange={(event) =>
                                this.setState({
                                    height:
                                        event.nativeEvent.contentSize.height <= 40
                                            ? 40
                                            : event.nativeEvent.contentSize.height
                                })}
                            style={{
                                height: Math.max(40, this.state.height),
                                width: wp('80%'),
                                borderWidth: 1,
                                alignSelf: this.state.height > 40 ? "flex-end":null,
                                borderColor: Colors.PRIMARY_GREEN,
                                borderRadius: 18,
                                paddingLeft: 15,
                                paddingTop: 10,
                                paddingRight: 15
                            }}
                            ref={'input'}
                            blurOnSubmit={true}
                            returnKeyType={'send'}
                            placeholder={'Write a message...'}
                            onSubmitEditing={() => {
                                if (this.props.role == 'Influencer') {
                                    this.send();
                                    this.refs.input.clear();
                                } else {

                                    this.setState({ visible: true });
                                }
                            }}
                        />
                    </View>
            </KeyboardAvoidingView>
        );
    }

我怎样才能开始实现一个在底部有所需填充的视野中的outgrow?

编辑:要清楚,这个问题不是关于如何制作自动增长输入字段。上面的代码已经完成了。它关于自动增长输入字段所在的容器,以及父容器、输入字段和按钮如何在键盘/键盘建议后面增长。目标是让文本输入始终直接显示在键盘上方,输入/按钮/容器的底部始终与键盘之间的空间相同,而高度仅向上扩展。

此处发布的问题类似:在 React Native 中,如何以一种可滚动的形式拥有多个不隐藏在键盘后面的多行文本输入?

标签: reactjsreact-nativereact-native-androidreact-native-ios

解决方案


您可以将父容器设置为“100%”高度并调整上部内容的大小,以便输入容器始终向上增长。

一个简单的实现如下。textInputHeight取自状态并在TextInput调整大小时设置。

  return (
    <KeyboardAvoidingView
      style={{height: '100%'}}
      behavior={Platform.OS === 'ios' ? 'position' : undefined}
      keyboardVerticalOffset={100}>
      <ScrollView
        style={{
          backgroundColor: '#777',
          height: height - (80 + textInputHeight),
        }}>
        <View style={{height: 1000}} />
      </ScrollView>
      <TextInput
        multiline={true}
        onLayout={(event) => {
          if (textInputHeight === 0) {
            setTextInputHeight(event.nativeEvent.layout.height);
          }
        }}
        onContentSizeChange={(event) => {
          setTextInputHeight(event.nativeEvent.contentSize.height);
        }}
      />
    </KeyboardAvoidingView>
  );


推荐阅读