Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto scrolling when focus on TextInput in scrollview in react native

Tags:

react-native

I have used many TextInput within ScrollView. I want when focus on a TextInput then scroll to this TextInput automatically. My code is following:

<View style = {{width: '100%', height: '85%'}}>
    <ScrollView showsVerticalScrollIndicator = {false} style = {{marginLeft: 10, marginRight: 10}} 
        keyboardShouldPersistTaps={true}
    >
        <KeyboardAvoidingView behavior={'padding'}>
            <View style = {styles.input_box}>
                <TextInput underlineColorAndroid = 'transparent' style = {styles.input_style}/>
            </View>
            <View style = {styles.input_box}>
                <TextInput underlineColorAndroid = 'transparent' style = {styles.input_style}/>
            </View>

            ... ... ...

            <View style = {styles.input_box}>
                <TextInput underlineColorAndroid = 'transparent' style = {styles.input_style}/>
            </View>
        </KeyboardAvoidingView>
    </ScrollView>

</View>

When I tap a TextInput, don't auto scroll to this TextInput component. so when tap lower TextInput then keyboard cover this TextInput component. So user have to scroll up to see this TextInput component.

Please anyone who know the way to fix this issue help me. Thanks so much.

like image 705
Water Flower Avatar asked Feb 24 '26 17:02

Water Flower


1 Answers

You can use KeyboardAvoidingView: It is a component to solve the common problem of views that need to move out of the way of the virtual keyboard. It can automatically adjust either its height, position, or bottom padding based on the keyboard height.

Just wrap your ScrollView with

https://reactnative.dev/docs/keyboardavoidingview

like image 85
fedarenchyk Avatar answered Feb 27 '26 06:02

fedarenchyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!