reactjs - 如何防止自动增长文本输入容器在 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?
编辑:要清楚,这个问题不是关于如何制作自动增长输入字段。上面的代码已经完成了。它关于自动增长输入字段所在的容器,以及父容器、输入字段和按钮如何在键盘/键盘建议后面增长。目标是让文本输入始终直接显示在键盘上方,输入/按钮/容器的底部始终与键盘之间的空间相同,而高度仅向上扩展。
解决方案
您可以将父容器设置为“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>
);
推荐阅读
- dart - Flutter: Where should I call SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark)
- reactjs - LocaleStorage 或 SessionStorage
- swift - 破坏性的 UIContextualAction 给出了糟糕的动画
- arrays - VBA比较不同长度的数组运行时错误9
- python - python / python3 - how to merge different locations for installed packages of anaconda and python?
- javascript - Adding doc value in mongodb
- c# - 关闭流错误消息 UTF 8 编码?
- javascript - Transform an array into a string with adding numbers of each element of an array?
- sql - 使用不同的日期集在 SQL 中查找计数
- swift - EKCalendar 显示 Apple 日历中没有的事件“以自然语言发现”