【问题标题】:Keyboard Avoiding View Shrinking键盘避免视图缩小
【发布时间】:2020-07-25 09:29:12
【问题描述】:

我希望我的页面向上滚动,以便用户可以正确输入其电子邮件和密码。因此,我使用的是 KeyboardAvoidingView。 但是,当我使用它时,它会缩小我的 textinput picture 而不是向上移动页面。如果没有键盘,我的应用看起来像这样picture

我的代码如下:

export default class LoginScreen extends React.Component {
    constructor(props) {
        super(props)
    }
    state = {
        email: '',
        password: ''
    }
    handleEmail = (text) => {
        this.setState({ email: text })
    }
    handlePassword = (text) => {
        this.setState({ password: text })
    }

    render() {
        return (
            <View style={styles.container}>
                    <ImageBackground source={require('../pics/main.png')} style={styles.background}>
                    <KeyboardAvoidingView behavior="padding" style={{ flex: 1}} >

                        <Text style={styles.title2}>WELCOME</Text>
                        <TextInput
                            style={styles.inputEmail}
                            placeholder="Enter Email"
                            onChangeText={this.handleEmail}
                        />
                        <TextInput
                            style={[styles.inputEmail, styles.inputPassword]}
                            placeholder="Enter password"
                            onChangeText={this.handlePassword}
                        />
                        <View style={styles.buttons}>
                            <TouchableOpacity style={styles.login} onPress={() => this.props.navigation.navigate('MainScreen')} >
                                <Text style={styles.loginText}>LOGIN</Text>
                            </TouchableOpacity>
                            <TouchableOpacity
                                style={styles.login} onPress={() => this.props.navigation.navigate('SignUpScreen')}>
                                <Text style={styles.loginText}>SIGN UP</Text>
                            </TouchableOpacity>
                        </View>
                        </KeyboardAvoidingView>

                    </ImageBackground>

            </View>
        );
    }
}

const styles = StyleSheet.create({
    area: {
        flex: 1
    },
    container: {
        flex: 1,
    },
    background: {
        flex: 1,
        resizeMode: "cover",
        width: '100%',
        height: '100%'
    },
    title2: {
        paddingTop: 10,
        color: color.primary,
        fontSize: 25,
        alignSelf: 'center',
        justifyContent: 'flex-start'
    },
    inputEmail: {
        borderWidth: 2,
        borderColor: color.primary,
        borderRadius: 10,
        width: '50%',
        height: '5%',
        marginTop: 100,
        alignSelf: 'center',
        backgroundColor: color.secondary
    },
    inputPassword: {
        marginTop: 10
    },
    buttons: {
        flexDirection: 'row',
        justifyContent: 'space-around',
        marginTop: 30,
        marginLeft: 70,
        marginRight: 70,
    },
    login: {
        borderColor: color.primary,
        borderRadius: 20,
        borderWidth: 2,
        width: '40%',
        height: '118%',
        backgroundColor: color.primary
    },
    loginText: {
        alignSelf: 'center',
        marginTop: 5,
        marginBottom: 1,
        color: 'white'
    }
});

知道我做错了什么吗?

更新:为避免缩小文本输入容器的宽度和高度,而不是百分比,而是实数:inputEmail --> 宽度:200,高度:35,

【问题讨论】:

  • 添加 KeyboardAvoidingView 作为页面的容器。代替 View,添加 KeyboardAvoidingView。我希望这会有所帮助
  • 不幸的是,它仍然做同样的事情,缩小电子邮件和密码文本框...
  • 我修复了缩小的问题,但是当键盘出现时输入文本框仍然没有上移

标签: reactjs react-native


【解决方案1】:

这是您的代码的解决方案,我已在此小吃中使用您提供的代码解决了这个问题。

Expo Snack with CSS fixes to avoid keyboard

【讨论】:

  • 我不知道它是否适合你,但在我的 iPhone 上它仍然和以前一样......
  • 是的,我试过了。我发现它正在缩小,因为输入框的高度以 % 为单位,所以我直接将它与数字放在一起。但是,当键盘出现时,我的键盘回避视图仍然没有显示输入文本框
  • 我更新了问题,如果我不清楚,您可以了解我的意思
  • 我正在使用 它仍然会缩小文本输入。 @Waheed Akhtar
  • 为什么要设置条件,即使两个条件返回相同的值也在哪个平台上出现此问题并请分享您的代码。
猜你喜欢
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
相关资源
最近更新 更多