14

I am trying to use the react-native-keyboard-aware-scroll-view so the keyboard doesn't cover my inputs.

For some reason it always thinks there is a keyboard active I guess because it always compresses everything.

Attached is a picture of what is happening as well as the code. Any chance anyone has any idea whats happening here? I've been playing around with it for awhile and have had no luck. I'm running react-native v 0.33 and react-native-keyboard-aware-scroll-view v 0.2.1.

https://www.npmjs.com/package/react-native-keyboard-aware-scroll-view

enter image description here

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';


class LoginIOS extends Component{

  constructor(props) {
    super(props);
    this.login = this.login.bind(this);
    this.renderScene = this.renderScene.bind(this);
    this.state={
      username: '',
      password: ''
    };
  }

  render() {
    return (
      <Navigator
          renderScene={this.renderScene}
          navigator={this.props.navigator}
          navigationBar={
            <Navigator.NavigationBar style={{backgroundColor: 'transparent'}}
                routeMapper={NavigationBarRouteMapper} />
          } />
    );
  }

  renderScene() {
    return (
    <KeyboardAwareScrollView>
      <View style={styles.container}>
        <Spinner visible={this.state.visible} />
        <StatusBar barStyle="light-content" hidden={true}/>
        <View style={styles.topContainer}>
          <View style={styles.bannerContainer}>
            <View style={{flexDirection: 'column', flex: 1, justifyContent: 'center', alignItems: 'center'}}>
              <Image style={styles.mark} source={require('***')} />
            </View>
          </View>
          <View style={styles.credentialContainer}>
                <View style={styles.inputContainer}>
                  <Icon style={styles.inputPassword} name="person" size={28} color="#FFCD00" />
                      <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                        <TextInput
                            style={styles.input}
                            placeholder="Username"
                            autoCorrect={false}
                            autoCapitalize="none"
                            returnKeyType="next"
                            placeholderTextColor="#e0e0e0"
                            onChangeText={(text) => this.setState({username: text})}
                            value={this.state.username}

                            >

                        </TextInput>
                      </View>
                </View>

                <View style={styles.inputContainer}>
                  <Icon style={styles.inputPassword} name="lock" size={28} color="#FFCD00" />
                      <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                        <TextInput
                            style={styles.input}
                            placeholder="Password"
                            returnKeyType="done"
                            autoCorrect={false}
                            secureTextEntry={true}
                            placeholderTextColor="#e0e0e0"
                            onChangeText={(text) => this.setState({password: text})}
                            value={this.state.password}
                            onSubmitEditing={(event)=> {
                              this.login();
                            }}
                            >
                        </TextInput>
                      </View>
                </View>
                <TouchableOpacity style={styles.forgotContainer}>
                </TouchableOpacity>
            </View>

        </View>

        <TouchableHighlight
          underlayColor="#D6AB00"
          onPress={this.login}
          style={styles.signInButtonContainer}>
          <Text style={styles.signInText}>Sign In</Text>
        </TouchableHighlight>

      </View>
    </KeyboardAwareScrollView>

    );
  }
wdlax11
  • 822
  • 3
  • 11
  • 29

14 Answers14

20

Personally solved this by using flex...

  <KeyboardAwareScrollView contentContainerStyle={{flex: 1}}>
    <View style={{flex: 1}}>
Dan Cork
  • 209
  • 2
  • 2
16

To make it working in android with expo I had to add a few more things, hope this will help

<KeyboardAwareScrollView extraScrollHeight={100} enableOnAndroid={true} 
   keyboardShouldPersistTaps='handled'>
       <ScrollView>
      </ScrollView>
</KeyboardAwareScrollView>
Furquan
  • 1,542
  • 16
  • 20
6

I solved this problem by using another lib. Not sure why the react-native-keyboard-aware-scroll-view doesn't work but if you implement the react-native-keyboard-aware-view you shouldn't have any problems.

https://www.npmjs.com/package/react-native-keyboard-aware-view

wdlax11
  • 822
  • 3
  • 11
  • 29
4

If anyone is still struggling with this issue. What worked for me was ensuring enableOnAndroid = true and setting a marginBottom inside the keyboardAwareScrollView.

<KeyboardAwareScrollView style={{width: "90%",marginBottom:150}} enableOnAndroid={true}>
Solly
  • 798
  • 3
  • 7
  • Wouldn't this fail if the keyboard is not exactly 150px? – Lazerbeak12345 Aug 31 '20 at 14:34
  • @Lazerbeak12345 it's not the greatest solution but it worked for the app I was working on. Implementing extraScrollHeight is a better option and one suggested by the documentation I believe. Try extraScrollHeight={150} – Solly Sep 01 '20 at 15:07
2

You can also use animated view as scroll view cannot have absolute views or fixed components. so listening to the keyboard event and making the adjustment would work fine.

onKeyboarDidShow(e) {
  //LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
  Animated.timing(this.relativeBottom, {
    duration: e.duration,
    toValue: Dimensions.get('window').height-em(64)-e.endCoordinates.height
  }).start()
}

onKeyboardWillHide(e) {
  //LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
  Animated.timing(this.relativeBottom, {
    duration: e.duration,
    toValue: Dimensions.get('window').height-em(64)
  }).start()
}

componentWillMount() {
    this._didShowListener = Keyboard.addListener('keyboardWillShow', this.onKeyboarDidShow.bind(this));
    this._willHideListener = Keyboard.addListener('keyboardWillHide', this.onKeyboardWillHide.bind(this));
}

componentWillUnmount() {
    this._didShowListener.remove();
    this._willHideListener.remove();
}
2

Settings that worked for me

    bounces={false}
    showsVerticalScrollIndicator={false}
    style={{marginBottom:150}}
    enableOnAndroid={true}
    scrollEnabled={true}
    extraScrollHeight={100}
    keyboardShouldPersistTaps='handled'
    scrollToOverflowEnabled={true}
    enableAutomaticScroll={true}
Aaron
  • 55
  • 6
1

I upgrade react-native version to 0.59.4 and the KeyboardAwareScrollView stop working as usual. Apparently this props are now mandatory:

<KeyboardAwareScrollView
scrollEnabled={true}
enableAutomaticScroll={true}>
nrm97
  • 77
  • 2
1

I was experiencing this issue on only ios but I was able to solve using

<KeyboardAwareScrollView
extrScrollHeight={-100}>
....your code
</KeyboardAwareScrollView>

Hope it helps!

Sunday David
  • 105
  • 5
1
 showsVerticalScrollIndicator={false}
bounces={false}
behavior={'padding'}
keyboardShouldPersistTaps="handled"
enableOnAndroid={true}
extraHeight={150}
extraScrollHeight={20}
alwaysBounceVertical={false}
keyboardOpeningTime={Number.MAX_SAFE_INTEGER}
scrollEnabled={props.scrollEnabled}
enableAutomaticScroll={true}
contentContainerStyle={props?.style}
style={{backgroundColor: Colors.background.primary}}
Umer Arain
  • 17
  • 2
0

Give parent view height like this

height:Dimensions.get('window').height

With flex:1 it will wrap up the contents.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Antier Solutions
  • 1,326
  • 7
  • 10
0

Just add the resetScrollToCoords, contentContainerStyle ( The stylesheet doesn't need to be named container ) and scrollEnabled ( you can set it to true I find it more useful like this ). It's going to work properly and fit well !

import React from 'react';
import { View, TextInput, Image } from 'react-native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
import styles from './styles';
import logo from './logo.png';

const Demo = () => {
  return (
    <KeyboardAwareScrollView
      style={{  }}
      resetScrollToCoords={{ x: 0, y: 0 }}
      contentContainerStyle={styles.container}
      scrollEnabled={false}
    >
        <Image source={logo} style={styles.logo} />
        <TextInput
          placeholder="Email"
          style={styles.input}
        />
        <TextInput
          placeholder="Username"
          style={styles.input}
        />
        <TextInput
          placeholder="Password"
          style={styles.input}
        />
        <TextInput
          placeholder="Confirm Password"
          style={styles.input}
        />
    </KeyboardAwareScrollView>
  );
};


let styles = StyleSheet.create({
    container: {
        flex: 1,
        flexDirection: "column"
    },
}),

export default Demo;
Olivier
  • 1
  • 3
0

With this it works. Also make sure that you have added the other necessary configuration as mentioned in the package.

_scrollToInput = (reactNode: any) => {
this.scroll._internalFiberInstanceHandleDEV.memoizedProps.scrollToFocusedInput(
  reactNode,
);

};

0

All these posts are outdated or do not work for me.

react-native-aware-scroll-view has lots of issues and is pretty much unmaintained.

I already answered this question here with what worked for me (might be a duplicate): https://stackoverflow.com/a/73678502/14056591

Ovidiu Cristescu
  • 821
  • 7
  • 19
0

What fixed it for me was removing flex: 1 from my styles and replacing it with flexGrow: 1.

brettinternet
  • 546
  • 7
  • 21
  • Can you please also give a brief explanation on the lines of: - how the 2 attributes differ from each other? - Why flexGrow works instead of flex? – NSaran May 17 '23 at 18:01