How to avoid collapsing of screen when keyboard opens in react native

大兔子大兔子 提交于 2020-03-24 00:30:35

问题


In my app the landing page is login screen. I've done it using native-base ui kit.The login screen contains a logo and below there will be 2 input fields for username and password also login button. The problem is that whenever i clicked on the input field the login form get collapse under the logo. I will share the corresponding screens for it. I've tried the following method

android:windowSoftInputMode="adjustResize" within activity tag in AndroidManifest.xml. But still getting the same screen. Please have a look at the screens.

  1. Normal screen behavior https://i.stack.imgur.com/ZFqfI.png
  2. After keyboard opens https://i.stack.imgur.com/tZr1i.png

I've also tried KeyboardAvoidngView .Following is the code

render() {
    return (
      <KeyboardAvoidingView behavior="padding" style={{flex:1}}>
      <Container>
        <StatusBar barStyle="light-content" />

          <View style={styles.logoContainer}>
            <ImageBackground source={launchscreenLogo} style={styles.logo} />
          </View>
          <View
            style={{
              flex:1,
              marginBottom: 200,
              backgroundColor: "transparent"
            }}
          >
            <Form style={{margin:10}}>
          <Item  style={{margin:10}}>
           <Icon active name='person' />
           <Input
             placeholder='Username'
             style={{marginLeft:18, color:'#000'}}
             value={this.state.username}
             onChangeText={username => this.setState({ username })}
             />
         </Item>
         <Item style={{margin:10}} >
          <Icon active name='mail' />
          <Input
          placeholder='Email'
          style={{marginLeft:18, color:'#000'}}
          value={this.state.email}
          onChangeText={email => this.setState({email})}/>

        </Item>
        <Item  style={{margin:10}}>
         <Icon active name='ios-lock' />
         <Input
         placeholder='Password' style={{marginLeft:18, color:'#000'}}
         value={this.state.password}
         onChangeText={password => this.setState({ password })}
         />

       </Item>
       <Item  style={{margin:10}}>
        <Icon active name='ios-lock' />
        <Input
        placeholder='Repeat Password'
        style={{marginLeft:18, color:'#000'}}
        value={this.state.r_password}
        onChangeText={r_password => this.setState({ r_password })}/>

      </Item>
        </Form>

        <Button
          style={{ backgroundColor: "#809fff", alignSelf: "center", elevation:20}}
          onPress={() => this.props.navigation.navigate("Anatomy")}
        >
          <Text style={{fontWeight:'bold',fontSize:18}}>REGISTER</Text>
        </Button>
        </View>


          <View style={{ marginBottom: 80 }}>

          </View>

      </Container>
      </KeyboardAvoidingView>

    );
  }

回答1:


You can use KeyboardAwareScrollView.

To add use npm i react-native-keyboard-aware-scroll-view --save

And then use it

<KeyboardAwareScrollView 
        keyboardShouldPersistTaps= 'always'
        style= {{ flex:1 }}  
        >
 <View>
  <TextInput />
 </View>
</KeyboardAwareScrollView>

For more info refer this. Hope this helps



来源:https://stackoverflow.com/questions/53626085/how-to-avoid-collapsing-of-screen-when-keyboard-opens-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!