Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard aware scroll view #13

Open
sobrinho opened this issue Aug 2, 2024 · 2 comments
Open

Keyboard aware scroll view #13

sobrinho opened this issue Aug 2, 2024 · 2 comments

Comments

@sobrinho
Copy link

sobrinho commented Aug 2, 2024

Hi, @EvanBacon !

How can we integrate that library with something like react-native-keyboard-controller or a similar one?

Quick example: clicking on a field near to the end of the page, the keyboard hides the input:

Screenshot 2024-08-02 at 14 57 21 Screenshot 2024-08-02 at 14 57 27
@sobrinho
Copy link
Author

sobrinho commented Aug 2, 2024

I would expect to click on "Senha" and the app scroll to a position like this:

Screenshot 2024-08-02 at 14 58 43

@ipassionforu
Copy link

Dear @sobrinho,

To address the issue of the keyboard obscuring input fields in your React Native application, I recommend implementing a keyboard-aware scroll view. Below are some detailed steps to assist you:

1. Utilize KeyboardAvoidingView

Begin by wrapping your entire form in a KeyboardAvoidingView. This component will help adjust the layout when the keyboard is displayed.

import React from 'react';
import { KeyboardAvoidingView, ScrollView, TextInput, StyleSheet } from 'react-native';

const YourComponent = () => {
  return (
    <KeyboardAvoidingView style={styles.container} behavior="padding">
      <ScrollView>
        <TextInput placeholder="Nome completo" style={styles.input} />
        <TextInput placeholder="CPF" style={styles.input} />
        <TextInput placeholder="Celular" style={styles.input} />
        <TextInput placeholder="Cidade" style={styles.input} />
        <TextInput placeholder="Senha" style={styles.input} secureTextEntry />
        <TextInput placeholder="Confirmação da senha" style={styles.input} secureTextEntry />
      </ScrollView>
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  input: {
    height: 50,
    borderColor: 'gray',
    borderWidth: 1,
    margin: 10,
    paddingHorizontal: 10,
  },
});

2. Consider the react-native-keyboard-aware-scroll-view Library

For enhanced functionality, I recommend using the react-native-keyboard-aware-scroll-view library, which automatically adjusts the scroll view when the keyboard appears.

Installation:

npm install --save react-native-keyboard-aware-scroll-view

Implementation:

import React from 'react';
import { KeyboardAvoidingView, StyleSheet } from 'react-native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';

const YourComponent = () => {
  return (
    <KeyboardAvoidingView style={styles.container}>
      <KeyboardAwareScrollView>
        {/* Include your input fields here */}
      </KeyboardAwareScrollView>
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

3. Conduct Testing on Various Devices

It is essential to test the implementation across different devices and orientations to ensure consistent keyboard behavior.

4. Adjust Scroll Behavior as Necessary

You may need to modify properties such as extraHeight or keyboardShouldPersistTaps in the KeyboardAwareScrollView to achieve the desired behavior.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants