无法弄清楚如何正确使用 KeyboardAvoidingView
Can't figure out how to properly use KeyboardAvoidingView
我正在使用 React Native 开发一个应用程序,我们有我们的设计师制作的屏幕布局。但我无法正确实现预期的行为。基本上它是一个带有一些文本输入和一个按钮的屏幕,当键盘出现时我需要一些东西来正确调整。以下是预期的屏幕:
所以当键盘出现时,按钮必须上升很多,文本输入都会上升一点,并且顶部的文本保持不变。屏幕在没有键盘的情况下看起来很完美,但现在当键盘出现时它什么也没做。我尝试了很多东西,但没有任何效果。现在是渲染方法:
render() {
const textInputBorderColor = this.state.hasError ? Colors.error : Colors.background;
const textInputCpfMarginTop = this.state.hasError ? 24 : 48;
return (
<View style = {styles.container}>
<KeyboardAvoidingView behavior='padding'>
<Text style = {styles.headerText}>Vamos começar!</Text>
<TextInput
value = {this.props.user.name}
onChangeText = {text => this.props.user.name = text}
placeholder = 'Insira aqui seu nome completo'
style = {[styles.textInputName, {borderColor: textInputBorderColor}]}
/>
<ErrorText show = {this.state.hasError} value = {this.state.errorMsg}/>
<TextInputMask
value = {this.props.user.cpf}
onChangeText = {text => this.props.user.cpf = text}
placeholder = 'e aqui seu CPF'
keyboardType = 'numeric'
type = 'cpf'
style = {[styles.textInputCpf, {borderColor: Colors.background, marginTop: textInputCpfMarginTop}]}
/>
<View style = {{marginTop: 202}}>
<DefaultButton
onPress = {this.onButtonPress}
btnLabel = 'Continuar'
disabled = {(this.props.user.name == '' || this.props.user.cpf.length != 14)}
/>
</View>
</KeyboardAvoidingView>
</View>
);
}
样式:
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFFFFF',
alignItems: 'center',
},
textInputName: {
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 16,
paddingBottom: ScreenDimensions.width * 0.01,
borderBottomWidth: 1,
marginTop: 96,
width: 321
},
textInputCpf: {
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 16,
paddingBottom: ScreenDimensions.width * 0.01,
borderBottomWidth: 1,
width: 321
},
headerText: {
marginTop: 66,
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 20,
color: '#000'
}
})
关于此组件 (keyboardAvoidingView) 的文档毫无价值...
非常感谢您的帮助!
试试这个:
const keyboardAvoidingViewBehaviour = (Platform.OS === 'ios') ? 'padding' : null;
使用KeyboardAvoiding
视图作为根组件,不需要额外的视图。
<KeyboardAvoidingView
behavior={keyboardAvoidingViewBehaviour}
style = {styles.container} // <-- have flex: 1 as one of the style props here
>
<Text style = {styles.headerText}>Vamos começar!</Text>
<TextInput
// ...
如果您的内容需要滚动,一个不错的选择是:
react-native-keyboard-aware-scroll-view
https://github.com/APSL/react-native-keyboard-aware-scroll-view
忘了 post 对我们有用的东西。经过一些测试,我们能够找到一个可行的解决方案。我们将文本输入包围在 ScrollView
中,因此在使用小屏幕设备时它可以正常工作,并且只用 KeyboardAvoidingView
包围按钮。两者都被外部 View
.
包围
这是一个例子:
<View style = {{flex: 1, alignItems: 'center', justifyContent: 'flex-end'}}>
<ScrollView
contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}
style={{ alignSelf: 'stretch' }}
>
# Your Text Inputs
</ScrollView>
<KeyboardAvoidingView
behavior='padding'
keyboardVerticalOffset={Platform.OS == 'ios' ? Header.HEIGHT + getStatusBarHeight() : Header.HEIGHT + 32}
style={{ flex: 1, justifyContent: 'flex-end', alignSelf: 'stretch', marginBottom: 16 }}
>
# Yout Button
</KeyboardAvoidingView>
</View>
旁注:在 ScrollViews 中使用文本输入和按钮时要当心(我的解决方案不是这种情况),因为有一个小问题,您无法点击打开键盘的按钮。
我正在使用 React Native 开发一个应用程序,我们有我们的设计师制作的屏幕布局。但我无法正确实现预期的行为。基本上它是一个带有一些文本输入和一个按钮的屏幕,当键盘出现时我需要一些东西来正确调整。以下是预期的屏幕:
所以当键盘出现时,按钮必须上升很多,文本输入都会上升一点,并且顶部的文本保持不变。屏幕在没有键盘的情况下看起来很完美,但现在当键盘出现时它什么也没做。我尝试了很多东西,但没有任何效果。现在是渲染方法:
render() {
const textInputBorderColor = this.state.hasError ? Colors.error : Colors.background;
const textInputCpfMarginTop = this.state.hasError ? 24 : 48;
return (
<View style = {styles.container}>
<KeyboardAvoidingView behavior='padding'>
<Text style = {styles.headerText}>Vamos começar!</Text>
<TextInput
value = {this.props.user.name}
onChangeText = {text => this.props.user.name = text}
placeholder = 'Insira aqui seu nome completo'
style = {[styles.textInputName, {borderColor: textInputBorderColor}]}
/>
<ErrorText show = {this.state.hasError} value = {this.state.errorMsg}/>
<TextInputMask
value = {this.props.user.cpf}
onChangeText = {text => this.props.user.cpf = text}
placeholder = 'e aqui seu CPF'
keyboardType = 'numeric'
type = 'cpf'
style = {[styles.textInputCpf, {borderColor: Colors.background, marginTop: textInputCpfMarginTop}]}
/>
<View style = {{marginTop: 202}}>
<DefaultButton
onPress = {this.onButtonPress}
btnLabel = 'Continuar'
disabled = {(this.props.user.name == '' || this.props.user.cpf.length != 14)}
/>
</View>
</KeyboardAvoidingView>
</View>
);
}
样式:
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFFFFF',
alignItems: 'center',
},
textInputName: {
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 16,
paddingBottom: ScreenDimensions.width * 0.01,
borderBottomWidth: 1,
marginTop: 96,
width: 321
},
textInputCpf: {
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 16,
paddingBottom: ScreenDimensions.width * 0.01,
borderBottomWidth: 1,
width: 321
},
headerText: {
marginTop: 66,
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 20,
color: '#000'
}
})
关于此组件 (keyboardAvoidingView) 的文档毫无价值...
非常感谢您的帮助!
试试这个:
const keyboardAvoidingViewBehaviour = (Platform.OS === 'ios') ? 'padding' : null;
使用KeyboardAvoiding
视图作为根组件,不需要额外的视图。
<KeyboardAvoidingView
behavior={keyboardAvoidingViewBehaviour}
style = {styles.container} // <-- have flex: 1 as one of the style props here
>
<Text style = {styles.headerText}>Vamos começar!</Text>
<TextInput
// ...
如果您的内容需要滚动,一个不错的选择是:
react-native-keyboard-aware-scroll-view
https://github.com/APSL/react-native-keyboard-aware-scroll-view
忘了 post 对我们有用的东西。经过一些测试,我们能够找到一个可行的解决方案。我们将文本输入包围在 ScrollView
中,因此在使用小屏幕设备时它可以正常工作,并且只用 KeyboardAvoidingView
包围按钮。两者都被外部 View
.
这是一个例子:
<View style = {{flex: 1, alignItems: 'center', justifyContent: 'flex-end'}}>
<ScrollView
contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}
style={{ alignSelf: 'stretch' }}
>
# Your Text Inputs
</ScrollView>
<KeyboardAvoidingView
behavior='padding'
keyboardVerticalOffset={Platform.OS == 'ios' ? Header.HEIGHT + getStatusBarHeight() : Header.HEIGHT + 32}
style={{ flex: 1, justifyContent: 'flex-end', alignSelf: 'stretch', marginBottom: 16 }}
>
# Yout Button
</KeyboardAvoidingView>
</View>
旁注:在 ScrollViews 中使用文本输入和按钮时要当心(我的解决方案不是这种情况),因为有一个小问题,您无法点击打开键盘的按钮。