關(guān)于 KeyboardAvoidingView

KeyboardAvoidingView 底層代碼實(shí)現(xiàn)git地址

01 - behavior='position' 模式

效果: 點(diǎn)擊輸入框時(shí),界面會(huì)向上抬升一個(gè)鍵盤(pán)的高度,
而且,不管鍵盤(pán)是否遮擋了輸入框,輸入框在編輯時(shí)都會(huì)向上抬升,這樣會(huì)產(chǎn)生抬升過(guò)度的效果,體驗(yàn)不是很好

    return (
      <KeyboardAvoidingView behavior='position' >
        <ScrollView style={{ height: height, paddingTop: 20, }}>
          <View style={{ flex: 1, justifyContent: 'center', }}>
            <View style={{ height: 600, backgroundColor: 'lightgray', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Top Area</Text>
            </View>
            <TextInput
              placeholder="<TextInput />"
              style={{ borderRadius: 5, borderWidth: 1, height: 44, paddingHorizontal: 10,
              }} />
            <View style={{ height: 200, backgroundColor: 'skyblue', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Bottom Area</Text>
            </View>
          </View>
        </ScrollView>
      </KeyboardAvoidingView>
    );

改善方法: 設(shè)置 keyboardVerticalOffset 屬性,調(diào)整KeyboardAvoidingView的偏移量,比如在監(jiān)聽(tīng)鍵盤(pán)事件中,獲取到鍵盤(pán)高度,然后刷新 keyboardVerticalOffset 的偏移值,還是可行的

解決界面抬升過(guò)度的問(wèn)題: 設(shè)置 keyboardVerticalOffset 為負(fù)數(shù),如:keyboardVerticalOffset = -150,效果是將界面往下移;設(shè)置 keyboardVerticalOffset 為正數(shù),如:keyboardVerticalOffset = 50,效果是將界面往上移

解決點(diǎn)擊背景區(qū)域無(wú)法回收鍵盤(pán)的問(wèn)題:KeyboardAvoidingView 包裹的子組件不是 ScrollView 的話,點(diǎn)擊背景區(qū)域無(wú)法回收鍵盤(pán),可以手動(dòng)添加 touchable 事件,實(shí)現(xiàn)鍵盤(pán)回收效果

解決界面沒(méi)有抬升效果的問(wèn)題:前提是 behavior='position' 的條件下, 如果KeyboardAvoidingView 包裹的子組件不是 ScrollView ,而是其它view,會(huì)出現(xiàn)界面沒(méi)有抬升的效果,可以在如果KeyboardAvoidingView 包裹的子組件中增加一層 ScrollView

return (
      <KeyboardAvoidingView behavior='position' keyboardVerticalOffset = {-150} >
        <ScrollView style={{ height: height, paddingTop: 20, }}>
          <View style={{ flex: 1, justifyContent: 'center', }}>
            <View style={{ height: 600, backgroundColor: 'lightgray', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Top Area</Text>
            </View>
            <TextInput
              placeholder="<TextInput />"
              style={{ borderRadius: 5, borderWidth: 1, height: 44, paddingHorizontal: 10,
              }} />
            <View style={{ height: 200, backgroundColor: 'skyblue', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Bottom Area</Text>
            </View>
          </View>
        </ScrollView>
      </KeyboardAvoidingView>
    );

02 - behavior='padding' 模式

效果: 界面沒(méi)有往上移

return (
      <KeyboardAvoidingView behavior='padding' keyboardVerticalOffset = {0} >
        <ScrollView style={{ height: height, backgroundColor: 'yellow' }}>
          <View style={{ flex: 1, justifyContent: 'center', }}>
            <View style={{ height: 600, backgroundColor: 'lightgray', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Top Area</Text>
            </View>
            <TextInput
              placeholder="<TextInput />"
              style={{ borderRadius: 5, borderWidth: 1, height: 44, paddingHorizontal: 10,
              }} />
            <View style={{ height: 200, backgroundColor: 'skyblue', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Bottom Area</Text>
            </View>
          </View>
        </ScrollView>
      </KeyboardAvoidingView>
    );

03 - behavior='height' 模式

效果:界面會(huì)正好抬升到和鍵盤(pán)高度平行的地方,但是整個(gè)界面的content高度會(huì)減少鍵盤(pán)的高度,所以當(dāng)鍵盤(pán)回收是,界面會(huì)出現(xiàn)一段空白

return (
      <KeyboardAvoidingView behavior='height' keyboardVerticalOffset = {0} >
        <ScrollView style={{ height: height, paddingTop: 20, }}>
          <View style={{ flex: 1, justifyContent: 'center', }}>
            <View style={{ height: 600, backgroundColor: 'lightgray', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Top Area</Text>
            </View>
            <TextInput
              placeholder="<TextInput />"
              style={{ borderRadius: 5, borderWidth: 1, height: 44, paddingHorizontal: 10,
              }} />
            <View style={{ height: 200, backgroundColor: 'skyblue', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Bottom Area</Text>
            </View>
          </View>
        </ScrollView>
      </KeyboardAvoidingView>
    );

另外,KeyboardAvoidingView 中 ScrollView 設(shè)置樣式為 flex: 1時(shí),界面無(wú)法顯示;如果不設(shè)置flex, 改設(shè)置height為屏幕高度時(shí),界面才會(huì)正常顯示出來(lái), 將 ScrollView 改成 View 也是一樣,需要設(shè)置 height 值,界面才會(huì)顯示出來(lái)

return (
      <KeyboardAvoidingView behavior='height' keyboardVerticalOffset = {0} >
        <ScrollView style={{ flex: 1, backgroundColor: 'yellow' }}>
          <View style={{ flex: 1, justifyContent: 'center', }}>
            <View style={{ height: 600, backgroundColor: 'lightgray', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Top Area</Text>
            </View>
            <TextInput
              placeholder="<TextInput />"
              style={{ borderRadius: 5, borderWidth: 1, height: 44, paddingHorizontal: 10,
              }} />
            <View style={{ height: 200, backgroundColor: 'skyblue', justifyContent: 'center', alignItems: 'center', }}>
              <Text style={{ fontSize: 28, color: '#998462', textAlign: 'center', }}>Bottom Area</Text>
            </View>
          </View>
        </ScrollView>
       </KeyboardAvoidingView>
    );
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過(guò)CocoaPods安裝項(xiàng)目名稱(chēng)項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明AI閱讀 16,240評(píng)論 3 119
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • 本文純屬扯淡. 想到自己以后可能瞎掉. 趕緊訓(xùn)練 siri. 今天腦子里突然蹦出來(lái)的 Cambodian, 當(dāng)時(shí)不...
    district10閱讀 627評(píng)論 0 0
  • 第一部分 導(dǎo)論 一、學(xué)習(xí)從猜測(cè)到科學(xué) 認(rèn)知心理學(xué)是人們解決各學(xué)科領(lǐng)域問(wèn)題的能力基礎(chǔ)。發(fā)展心理學(xué)告訴人們兒童是可塑的...
    胡永強(qiáng)老師閱讀 516評(píng)論 0 0
  • 來(lái)這個(gè)城市快一個(gè)月了,每天藍(lán)天,偶爾白云。 天藍(lán)得不像話,像畫(huà)。 陽(yáng)光照在身上,像置身暖爐邊。21度,真的是初夏的...
    漫漫無(wú)憂閱讀 163評(píng)論 0 0

友情鏈接更多精彩內(nèi)容