【发布时间】:2020-08-20 18:43:46
【问题描述】:
目标:创建一个文本输入字段,该字段会随着输入行的扩展而响应扩展,直到某个点,就像大多数消息传递应用程序一样。
问题:当输入扩展超过 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?
编辑:要清楚,这个问题不是关于如何制作自动增长的输入字段。上面的代码已经完成了。它关于自动增长输入字段所在的容器,以及父容器、输入字段和按钮如何在键盘/键盘建议之后增长。 目标是让文本输入始终直接显示在键盘上方,输入/按钮/容器的底部在键盘之间始终保持相同的间距,而高度仅向上扩展。
【问题讨论】:
-
查看此链接。这可能会有所帮助.. stackoverflow.com/a/56292825/6169668
-
没有帮助,如您所见,我同时使用了
multiline属性和onContentSizeChange
标签: reactjs react-native react-native-android react-native-ios