Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent KeyboardAvoidingView from causing content to overlap (React Native)?

I am trying to create a KeyboardAvoidingView area for a signup form. I have gotten the component to a place where the actual keyboard adjustment is decent on iOS and Android.

However, instead of adding more height to the bottom of the view and scrolling up, the KeyboardAvoidingView seems to be simply compressing the height.

Here is the resulting effect on Android:

Before keyboard adjustment:

enter image description here

After keyboard adjustment:

enter image description here

Here's the code for the component:

<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>

And the styles:

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,
  },
})

How can I fix this, so that the content does not overlap?

like image 225
j_d Avatar asked Nov 18 '22 18:11

j_d


1 Answers

I ran into the same issue. I wrapped the children of KeyboardAvoidingView in a View component and then set the minHeight of that component using Dimennsions.get('window').height. See your example below:

<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>

Then the style:

const windowHeight: Dimensions.get('window').height;

export default StyleSheet.create({
  wrapper: {
    minHeight: windowHeight,
  },
    ...
});

You may need to add additional necessary styles such as flex etc. to the wrapper.

like image 99
Harry Riley Avatar answered Jan 02 '23 14:01

Harry Riley