键盘避免视图不适用于反应导航

时间:2021-06-27 12:02:13

标签: react-native react-native-android react-navigation-v5

import React from 'react';
 import { View, KeyboardAvoidingView, TextInput, StyleSheet, Text, Platform, TouchableWithoutFeedback, Button, Keyboard  } from 'react-native';
    
    const KeyboardAvoidingComponent = () => {
      return (
        <KeyboardAvoidingView
          behavior={Platform.OS === "ios" ? "padding" : "height"}
          style={styles.container}
        >
          <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
            <View style={styles.inner}>
              <Text style={styles.header}>Header</Text>
              <TextInput placeholder="Username" style={styles.textInput} />
              <View style={styles.btnContainer}>
                <Button title="Submit" onPress={() => null} />
              </View>
            </View>
          </TouchableWithoutFeedback>
        </KeyboardAvoidingView>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1
      },
      inner: {
        padding: 24,
        flex: 1,
        justifyContent: "space-around"
      },
      header: {
        fontSize: 36,
        marginBottom: 48
      },
      textInput: {
        height: 40,
        borderColor: "#000000",
        borderBottomWidth: 1,
        marginBottom: 36
      },
      btnContainer: {
        backgroundColor: "white",
        marginTop: 12
      }
    });

export default KeyboardAvoidingComponent;
<块引用>

示例代码

当我在 App.js 上运行此代码时,它运行良好,但在 Stack navigator 内的另一个屏幕上无法运行

我尝试过其他应用程序和同样的问题,任何人都可以帮忙

  • 反应原生 0.64.2
  • @react-navigation/native": "^5.9.4"
  • @react-navigation/stack": "^5.14.5"

0 个答案:

没有答案