【问题标题】:How to force react native content to ignore keyboard?如何强制反应原生内容忽略键盘?
【发布时间】:2018-10-13 19:49:26
【问题描述】:

我尝试同时使用 KeyboardAvoidingView 和 ScrollView 来防止我的内容在键盘存在时被挤压(向上推)。我已经尝试使用填充、高度和位置来处理我的行为,但没有任何效果。有人可以告诉我如何强制我的内容忽略键盘而不被推高吗??

return (
    <View style={{height: '100%', backgroundColor: '#D6D6D6', position: 'relative'}}>
        <View style={styles.wrapper}>
            <View style={{height:'100%', borderRadius: 7}}>
                <View style={styles.container}>
                    <ScrollView style={{borderRadius: 7}}
                        horizontal
                        showsHorizontalScrollIndicator={false}
                        scrollEventThrottle={10}
                        pagingEnabled
                        onScroll={
                            Animated.event(
                                [{nativeEvent: {contentOffset: {x: this.animVal}}}]
                            )
                        }
                    >
                        {imageArray}
                    </ScrollView>
                    <View style={styles.listViewContainer}>
                        <TouchableOpacity style={styles.listView} onPress={() => Actions.pop()}>
                            <View style={{flex: 1, flexBasis: 22}}>{listIcon}</View>
                            <View style={{flex: 2, flexBasis: 57}}><Text style={{color: '#fff'}}>List View</Text></View>
                        </TouchableOpacity>
                    </View>
                    <View style={styles.circleContainer}>
                        {circleArray}
                    </View>
                </View>
                <View style={styles.productsSection}>
                    <Text style={styles.title}>{prodDesc}</Text>
                    <Text style={styles.desc}>{prodBrand}</Text>
                    <Text style={styles.desc}>Item: {prodId || ''}</Text>
                    <Text style={[styles.desc, {marginBottom: 15}]}>Category: {prodCat}</Text>
                    <Table borderStyle={{borderWidth: 0}}>
                        <Rows data={rows}/>
                    </Table>
                </View>
                <View style={styles.bodyFooter}>
                    <QuantityCounter style={{width: '100%', display: 'block', marginRight: 20}} data={{productId: prodId}} />
                </View>
            </View>
        </View>
        <View style={styles.footer}>
            <View style={styles.cartContainer}>
                {cartIcon}
                <Text style={{color: '#3A3A3A', fontSize: 14}}>18 items</Text>
            </View>
            <TouchableOpacity style={styles.viewCartButtonContainer} onPress={() => this.cartRedirect() }>
                <Text style={{color: '#fff', fontSize: 15, marginTop: '5%'}}>View Cart</Text>
            </TouchableOpacity>
        </View>
        <Header/>
    </View >
);

这是我的主要风格:

var styles = StyleSheet.create({
    wrapper: {
        backgroundColor: '#E6E6E6',
        marginVertical: 15,
        marginHorizontal: 10,
        borderRadius: 7,
        elevation: 3,
        maxHeight: '80%',
        flexShrink: 1,
        zIndex: 0,
        marginTop: 75
    },
    container: {
        flex: 1.7,
        justifyContent: 'space-between',
        alignItems: 'center',
        height: '50%',
        borderRadius: 7
    },
    footer: {
        justifyContent:'space-between',
        alignItems: 'center',
        height: '10%',
        backgroundColor: '#E6E6E6',
        paddingVertical: 15,
        paddingHorizontal: 17,
        flexDirection: 'row',
        borderStyle: 'solid',
        borderTopColor: '#8E8E93',
        borderTopWidth: 1
    },
    cartContainer: {
        flexDirection: 'row',
        width: '35%'
    },
    viewCartButtonContainer: {
        backgroundColor: '#356FAF',
        height: '90%',
        width: '45%',
        padding: 20,
        alignItems: 'center',
        justifyContent: 'center',
        borderRadius: 3
    },
    bodyFooter: {
        backgroundColor: '#F6F6F6',
        justifyContent: 'center',
        flex: 0.45,
        borderTopColor: '#D6D6D6',
        borderTopWidth: 1,
        borderStyle: 'solid',
        borderBottomRightRadius: 7,
        borderBottomLeftRadius: 7
    },
    circleContainer: {
        position: 'absolute',
        zIndex: 2,
        bottom: 10,
        left: 10,
        flexDirection: 'row',
    },
    listViewContainer: {
        position: 'absolute',
        zIndex: 10,
        top: 0,
        right: 0,
        justifyContent: 'center'
    },
    listView: {
        display: 'flex',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center',
        borderTopRightRadius: 3,
        backgroundColor: '#000',
        paddingVertical: 5,
        paddingHorizontal: 10
    },

没有键盘的样子:

键盘的样子:

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    在 React Native 中处理切换键盘时的视图行为可能是一件棘手的事情。此类问题有多种可能的解决方案,但在这种情况下,解决方案是这样的:

    不要在你的组件上使用style={{height:'100%'}},而是尝试使用Dimensions:

    import {Dimensions} from 'react-native'; 
    const { height } = Dimensions.get('window');
    

    并在正确的组件中指定style={{ height }}


    如果其他人偶然发现了这个问题,可能值得一试的另一件事:

    React Native for Android 在 Android 清单中定义了一些默认设置。如果您不使用 Expo(或 CRNA),您可以通过更改 windowSoftInputMode 规则来更改 AndroidManifest.xml 中键盘的行为。

    尝试将android:windowSoftInputMode="adjustResize" 更改为android:windowSoftInputMode="adjustPan"android:windowSoftInputMode="adjustNothing"。如果这不能给你想要的效果,你可以尝试使用其他一些选项 (See here)。

    【讨论】:

    • 我将我的 android:windowSoftInputMode 从 adjustResize 切换到了 adjustPan 但它仍然没有工作。我需要在 Android 清单文件中进行重建吗??
    • 是的,如果更改原生代码(android或ios文件夹内的代码)需要重新构建
    • 我已经重建,但没有任何改变。当键盘存在时,我仍然会推送我的内容:(
    • 即使使用adjustPan 并且没有KeyboardAvoidingView?可以试试改成adjustNothing吗?
    • 您可以尝试这样做吗:不要在被推高的组件上使用 style={{height:'100%'}},而是尝试使用尺寸。 `从'react-native'导入{Dimensions}; const { 宽度,高度 } = Dimensions.get('window');并指定 style={{height}}。这是我最后的想法,但是如果这不起作用,我不知道如何解决这个问题。
    【解决方案2】:

    你应该使用"none" for android的try行为,如果你不想变小,你可以在manifest文件中设置android:windowSoftInputMode="adjustPan"

    如果仍然遇到任何错误检查 react-native-keyboard-aware-scrollview here 在 npm 上。

    【讨论】:

    • 我将我的 android:windowSoftInputMode 从 adjustResize 切换到 adjustPan 并切换回使用 KeyboardAvoidingView 并使行为为“无”,但它仍然不起作用。我的内容仍然被挤压(向上推),但现在我也收到警告,“none”不是有效值(只有填充、高度和位置)。
    • 哦,我的错,它应该是 null 尝试像 behavior={Platform.OS == "ios" 之类的东西? 'height' : null} 对于两者。是的。当您更改清单时,请确保重新安装。不知道你有没有这么重视。
    • “两者”是什么意思?我该怎么做 behavior={Platform.OS == "ios" ? 'height' : null} 两者都有吗?两者是什么?我以为“行为”只是 K​​eyboardAvoidingView 上的一个道具???
    • 我的意思是,通常人们在这两个平台上都使用 React-native。他们更喜欢在文档中注意:Android 和 iOS 都以不同的方式与这个道具交互。如果根本没有提供任何行为道具,Android 可能会表现得更好,而 iOS 则相反。所以通过这种情况,它会在 ios 上取高度或任何你想要的东西,在 android 上取 null。
    • 我尝试了 behavior={Platform.OS == "ios" ? 'height' : null} 但我没有变量 Platform 所以我不能使用它。不过,我分别尝试了 height 和 null 。 behavior="height" 然后 behavior="null" 但两者都不起作用。他们仍然将内容推高,并且“null”给了我一个警告,说它不是一个有效的值。
    【解决方案3】:

    我遇到了类似的问题并通过更改解决了 android:windowSoftInputMode="adjustPan"

    在 android 清单中。 还清理和重建。这可能有效

    【讨论】:

      【解决方案4】:

      删除绝对位置,相信我会正常工作

      【讨论】:

        【解决方案5】:

        在某些情况下,如果您想保留默认清单 您可以在 Scrollview 中移动元素,这可能会有所帮助。

        以这种方式为我解决了问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-24
          • 2011-04-24
          • 1970-01-01
          相关资源
          最近更新 更多