j_d*_*j_d 5 javascript css react-native
我正在尝试KeyboardAvoidingView为注册表单创建一个区域。我已经把组件放到了一个可以在 iOS 和 Android 上进行实际键盘调整的地方。
然而,KeyboardAvoidingView与其向视图底部添加更多高度并向上滚动,似乎只是压缩了高度。
以下是在 Android 上的效果:
键盘调整前:
键盘调整后:
这是组件的代码:
<KeyboardAvoidingView keyboardVerticalOffset={20} behavior={Platform.OS === 'ios' ? 'padding' : null} style={mainWithFooter.container}>
<View style={mainWithFooter.main}>
<Text style={material.display1}>Create Your Account</Text>
</View>
<View style={mainWithFooter.footer}>
<Input
placeholder='First name'
onChangeText={t => updateSignupForm('firstName', t)}
/>
<Input
placeholder='Last name'
onChangeText={t => updateSignupForm('lastName', t)}
/>
<Input
placeholder='Email'
keyboardType='email-address'
autoCapitalize='none'
onChangeText={t => updateSignupForm('email', t)}
/>
<Input
placeholder='Password'
secureTextEntry
onChangeText={t => updateSignupForm('password', t)}
/>
<Button
text='Create Account'
onPress={signup}
primary
disabled={!signupFormIsValid}
block
/>
</View>
</KeyboardAvoidingView>
Run Code Online (Sandbox Code Playgroud)
和样式:
export default StyleSheet.create({
container: {
display: 'flex',
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
padding: 30,
minHeight: '100%',
},
main: {
flex: 1,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
marginBottom: 20,
},
footer: {
width: '100%',
flex: 0,
},
})
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题,以便内容不重叠?
小智 2
我遇到了同样的问题。我将 的子级包装KeyboardAvoidingView在一个View组件中,然后minHeight使用 来设置该组件的Dimennsions.get('window').height。请参阅下面的示例:
<KeyboardAvoidingView keyboardVerticalOffset={20} behavior={Platform.OS === 'ios' ? 'padding' : null} style={mainWithFooter.container}>
<View style={mainWithFooter.wrapper}> ** <-- wrapper here. **
<View style={mainWithFooter.main}>
<Text style={material.display1}>Create Your Account</Text>
</View>
<View style={mainWithFooter.footer}>
<Input
placeholder='First name'
onChangeText={t => updateSignupForm('firstName', t)}
/>
...
</View>
</View>
</KeyboardAvoidingView>
Run Code Online (Sandbox Code Playgroud)
然后是风格:
const windowHeight: Dimensions.get('window').height;
export default StyleSheet.create({
wrapper: {
minHeight: windowHeight,
},
...
});
Run Code Online (Sandbox Code Playgroud)
您可能需要flex向包装器添加其他必要的样式,例如等。