美文网首页react-native React Native
react-Native键盘收起、textInput键盘遮挡问题

react-Native键盘收起、textInput键盘遮挡问题

作者: m_麻 | 来源:发表于2019-02-11 10:57 被阅读0次

点击界面任意处键盘收起

import {
    Keyboard,
    TouchableOpacity,
} from 'react-native';
 <TouchableOpacity 
onPress={() => { Keyboard.dismiss(); }} 
activeOpacity={1} style={{flex: 1}}>
            </TouchableOpacity >

键盘遮挡问题:
1.使用KeyboardAvoidingView

import {KeyboardAvoidingView} from 'react-native';
 @observable top = getPixel(100);
 <ScrollView>
                        <KeyboardAvoidingView behavior='position' keyboardVerticalOffset={this.top}>
                            <LoginInput 
                                onFocus={() => { this.top = getPixel(40); }}/>
                            <LoginInput 
                                onFocus={() => { this.top = getPixel(100); }}/>
                            <LoginInput
                                onFocus={() => { this.top = getPixel(100); }}/>
                        </KeyboardAvoidingView>
                    </ScrollView>

LoginInput.js

onFocus=() => {
        const {onFocus} = this.props;
        onFocus && onFocus();
    }

render(){
    return(
            <TextInput onFocus={this.onFocus}/>
    )
}

2.使用第三方库:KeyboardAwareScrollView

 import {KeyboardAwareScrollView} from 'react-native-keyboard-aware-scroll-view';
 <KeyboardAwareScrollView
                    extraHeight={140}
                    keyboardOpeningTime={10}
                    style={{
                        marginTop: getTitlePixel(64),
                    }}>
<View></View>
</KeyboardAwareScrollView>

参考借鉴:
KeyboardAwareScrollView详细见react-native-keyboard-aware-scroll-view git

相关文章

网友评论

    本文标题:react-Native键盘收起、textInput键盘遮挡问题

    本文链接:https://www.haomeiwen.com/subject/skafeqtx.html