ios - React Native KeyboardAvoidingView 无法正常工作

标签 ios react-native tpkeyboardavoiding

我正在尝试将 KeyboardAvoidingView 与 behavior="padding"一起使用。

当我尝试在 TextInput 中输入任何文本时,TextInput 字段没有向上移动。我在最后添加了一个向上移动的小 View ,但它上面的 View 。

我还有带偏移量的 KeyboardAvoidingView 高度属性。它工作的组件很少,例如 2 个 TextInputs。但是当我添加所有组件时,UI 变得疯狂并且表现困惑。

知道这里发生了什么吗?

这里是 link我遵循的教程。

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'
}, });

最佳答案

我使用了 KeyboardAvoidingView,它也不起作用。 我找到了这个解决方案,您可以获取基本代码。

安装

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

用法

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

你可以找到它here

关于ios - React Native KeyboardAvoidingView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48744493/

相关文章:

ios - 尝试在我的应用程序中使用 TPKeyboardAvoding Scroll

ios - 如何在swift项目中使用TPKeyboardAvoiding?

ios - UIImage drawInRect

ios - 安装 XCode 4.5 会删除 iOS Simulator 5.1 吗?

react-native - 水平对齐 2 个文本标签

react-native - 我如何从 Appjs 中的商店获取状态?

javascript - 如何从 react-native 中的 GET 请求解析 JSON 数组?

ios - (iOS) iBeacon 唤醒如何工作?

ios - 如何获取iOS核心数据中每个实体的最后一个条目