react-native 鍵盤遮擋輸入框


Android上已經自動對鍵盤遮擋輸入框做了處理,所以我們只需要關注ios。

1.首先引入 KeyboardAvoidingView

import { KeyboardAvoidingView } from 'react-native';

2.然后在頁面的最外層加上 KeyboardAvoidingView

render(){
    return <KeyboardAvoidingView behavior={'padding'} style={{flex: 1}}>
        {/*具體頁面內容*/}
    </KeyboardAvoidingView>
}

如果適配ios和Android,可以將頁面提取出來

    getPageView = () => {
        //return 具體頁面內容
    }
    getPlatformView = () => {
        if (Platform.OS === 'ios') {
            return <KeyboardAvoidingView behavior={'padding'} style={{flex: 1}}>
                    {this.getPageView()}
            </KeyboardAvoidingView>
        } else {
            return this.getPageView();
        }
    };

    render() {
        return this.getPlatformView();
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM