简体   繁体   English

React Native KeyboardAvoidingView 无法正常工作

[英]React Native KeyboardAvoidingView not working properly

I am trying to use the KeyboardAvoidingView with behavior="padding".我正在尝试使用带有行为 =“填充”的 KeyboardAvoidingView。

When I am trying to enter any text in TextInput, the TextInput field is not moving up.当我尝试在 TextInput 中输入任何文本时,TextInput 字段没有向上移动。 I have added a small view in the end which is moving up but the the view above it.我最后添加了一个向上移动的小视图,但它上面的视图。

I have also with KeyboardAvoidingView height property with offset.我也有带有偏移量的 KeyboardAvoidingView 高度属性。 It was working few components like 2 TextInputs.它工作的组件很少,例如 2 TextInputs。 But when I add all the components the UI goes insane and behave jumbled up.但是当我添加所有组件时,UI 变得疯狂并且行为混乱。

Any any idea whats happening over here?知道这里发生了什么吗?

Here the link of tutorial which I have followed.这是我遵循的教程链接

render() {
    return (
        <View style={styles.backgroundContainer}>
            <Loader
                loading={this.state.isLoading} />
            <KeyboardAvoidingView
                keyboardVerticalOffset={10}
                style={styles.mainContainer}
                behavior='padding' >
                <View style={styles.formContainer}>
                    <View style={[styles.centerContainer, { marginTop: 40 }]}>
                        <Image source={require('./../../Resources/logo.png')} />
                        <Text style={{ fontWeight: 'bold', color: 'gray', fontSize: 25 }}>AppName</Text>
                        <Text style={styles.loginMsg}> Login to your Account </Text>
                    </View>
                    <View style={styles.inputFieldsContainer}>
                        <Image style={{ width: 30, height: 30, margin: 5 }} source={require('./../../Resources/logo.png')} />
                        <TextInput
                            placeholder='Email'
                            returnKeyType='next'
                            keyboardType='email-address'
                            onChangeText={(value) => this.setState({ userEmail: value })}
                            onSubmitEditing={() => this.passwordInput.focus()}
                            style={styles.inputFields} />
                    </View>
                    <View style={styles.inputFieldsContainer}>
                        <Image style={{ width: 30, height: 30, margin: 5, }} source={require('./../../Resources/logo.png')} />
                        <TextInput
                            returnKeyType='go'
                            secureTextEntry
                            placeholder='Password'
                            ref={(input) => this.passwordInput = input}
                            onChangeText={(value) => this.setState({ password: value })}
                            style={styles.inputFields} />
                    </View>
                    <View style={styles.buttonContainer}>
                        <Button
                            fontSize='8'
                            color='gray'
                            title='Remember Me'
                            onPress={() => {
                                console.log('Remember Me Clicked');
                            }} />
                        <Button
                            color='gray'
                            title='Forgot Password?'
                            onPress={() => {
                                console.log('Forgot Password Clicked');
                            }} />
                    </View>
                    <TouchableOpacity
                        style={styles.buttonLogin}
                        onPress={() => {
                                console.log('Login Clicked');
                        }}
                    >
                        <Text style={{ fontSize: 20, fontWeight: 'bold', color: 'white' }}>Login</Text>
                    </TouchableOpacity>
                    <View style={{ height: 1, backgroundColor: 'gray', marginTop: 20, marginBottom: 1 }}>
                    </View>
                    <View style={[styles.centerContainer, { marginBottom: 10 }]}>
                        <Text style={styles.loginMsg}>Don't have a Account</Text>
                        <TouchableOpacity
                            style={styles.buttonRegister}
                            onPress={() => navigate('Register')} >
                            <Text style={styles.buttonRegisterText}>REGISTER NOW</Text>
                        </TouchableOpacity>
                    </View>
                </View>
                <View style={{ height: 10, backgroundColor: '#628499', }}>
                </View>
            </KeyboardAvoidingView>
        </View>
    );
}
const styles = StyleSheet.create({
backgroundContainer: {
    flex: 1, backgroundColor: 'green'
},
mainContainer: {
    flex: 1, margin: 25,
    borderWidth: 1, borderRadius: 5, borderColor: 'gray',
    backgroundColor: 'white',
    justifyContent: 'space-between'
},
formContainer: {
    flex: 1, paddingLeft: 25, paddingRight: 25
},
centerContainer: {
    alignItems: 'center', marginBottom: 10,
},
loginMsg: {
    margin: 10,
    fontSize: 20, color: 'gray'
},
inputFieldsContainer: {
    flexDirection: 'row', margin: 10,
    borderWidth: 1, borderRadius: 5, borderColor: 'gray',
},
inputFields: {
    flexGrow: 1,
    marginTop: 5, marginBottom: 5,
    height: 30,
    backgroundColor: 'rgba(255,255,255,0.4)',
    paddingHorizontal: 10,
},
buttonContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between'
},
buttonLogin: {
    alignItems: 'center', height: 40, marginTop: 10, marginLeft: 50, marginRight: 50, padding: 5,
    backgroundColor: '#2980b9',
    borderWidth: 1, borderRadius: 5, borderColor: 'gray'
},
buttonRegister: {
    alignItems: 'center', height: 40,
    marginLeft: 50, marginRight: 50
},
buttonRegisterText: {
    fontSize: 20, fontWeight: 'bold', color: 'gray'
},
loading: {
    position: 'absolute',
    left: 0, right: 0, top: 0, bottom: 0,
    alignItems: 'center', justifyContent: 'center'
}, });

I used KeyboardAvoidingView, it also doesn't work.我使用了 KeyboardAvoidingView,它也不起作用。 I found this solution, You can take the base code.我找到了这个解决方案,您可以使用基本代码。

Installation安装

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

Usage用法

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
<KeyboardAwareScrollView>
  <View>
    <TextInput />
  </View>
</KeyboardAwareScrollView>

You can find ithere你可以在这里找到

<KeyboardAvoidingView
  style={styles.mainContainer}
  behavior="padding"
  enabled
>
  <View>.......................</View>
</KeyboardAvoidingView>

'Enabled or disabled KeyboardAvoidingView' should be add.应添加“启用或禁用 KeyboardAvoidingView”。

Keep in mind is always your top Parent with flex:1 then the child is then you text input container if you use this method it work always its test method.请记住,使用 flex:1 的父级始终是您的顶级父级,然后子级是您的文本输入容器,如果您使用此方法,则它始终是其测试方法。

<KeyboardAvoidingView style={{  flex: 1 }}
    behavior={Platform.OS === "ios" ? "position" : null} enabled>
      <ScrollView>
        <View>
          <View >
            <Text maxFontSizeMultiplier={1.5} >
              Sign in to your account{" "}
            </Text>
             <View
                behavior="padding"
                enabled
              >
                <TextInput
                  placeholder="Email address"
                  placeholderTextColor={Colors.grey}
                  style={styles.textInput}
                  onChangeText={(e) => setEmail(e.trim())}
                  autoCapitalize="none"
                  returnKeyType={"done"}
                />
              </View>
        </View>
      </ScrollView>
</KeyboardAvoidingView>

The way it worked for me was to use position as behavior and -70 as vertical offset.它对我有用的方式是使用位置作为行为和 -70 作为垂直偏移。 Any non negative integer was creating a huge unused space when the keyboard pops up.当键盘弹出时,任何非负整数都会创建一个巨大的未使用空间。

<KeyboardAvoidingView
    style={styles.container}
    behavior={Platform.OS === 'ios' ? 'position' : 'height'}
    keyboardVerticalOffset={Platform.OS === 'ios' ? -70 : 70}
    enabled>
     <ScrollView>
       ...
     </ScrollView>
</KeyboardAvoidingView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 KeyboardAvoidingView 在 iOS 上无法正常工作,即使我尝试了包括文档在内的所有解决方案,也会做出本机反应 - KeyboardAvoidingView not working properly on iOS react native, expo even if I tried all solutions including the documentation KeyboardAvoidingView 在 iOS 中无法正常工作 - KeyboardAvoidingView not working properly in iOS 在 React Native 中,如何在 iOS 中将 KeyboardAvoidingView 与 Modal 一起使用? - In React Native, How Can I Use KeyboardAvoidingView with a Modal in iOS? React Native - 如何使 ScrollView 中的 KeyboardAvoidingView 适用于所有设备? - React Native - How to make KeyboardAvoidingView inside a ScrollView work for all devices? zIndex在IOS中的react-native中无法正常工作 - zIndex not working properly in react-native in IOS KeyboardAvoidingView在IOS上无法正常工作 - KeyboardAvoidingView not working as expected on IOS 即使在React-Native中使用KeyboardAvoidingView,键盘也会阻止TextInput字段 - keyboard is blocking TextInput field even when using KeyboardAvoidingView in React-Native setState 在发布模式下无法正常工作 React Native iOS - setState not working properly in release mode React native iOS react-native-keyboard-aware-scroll-view 无法正常工作 - react-native-keyboard-aware-scroll-view not working properly 反应本机SliderIOS无法正确呈现 - React Native SliderIOS not rendering properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM