《React-Native系列》42、键盘遮挡问题官方处理方法KeyboardAvoidingView

来源:互联网 发布:rbf神经网络 python 编辑:程序博客网 时间:2024/06/05 20:03

今天,偶然翻Github发现了KeyboardAvoidingView,原来FaceBook的开发者们也意识到了键盘遮挡的问题。

从0.31版本开始,提供了官方的解决方案。

源码地址如下:https://github.com/facebook/react-native/blob/master/Libraries/Components/Keyboard/KeyboardAvoidingView.js


使用方法如下:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <KeyboardAvoidingView behavior='padding'>  
  2.   <View style={styles.textInputContainer}>  
  3.     <TextInput  
  4.       value={this.state.data}  
  5.       style={styles.textInput}  
  6.       onChangeText={this.handleChangeData}  
  7.     />  
  8.   </View>  
  9. </KeyboardAvoidingView>  


KeyboardAvoidingView的主要属性behavior  PropTypes.oneOf(['height', 'position', 'padding']) 


这个三个属性什么意思?我也不知道,那就一个个试吧!

Demo如下:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Copyright (c) 2013-present, Facebook, Inc. 
  3.  * All rights reserved. 
  4.  * 
  5.  * This source code is licensed under the BSD-style license found in the 
  6.  * LICENSE file in the root directory of this source tree. An additional grant 
  7.  * of patent rights can be found in the PATENTS file in the same directory. 
  8.  * 
  9.  * @providesModule KeyboardAvoidingViewExample 
  10.  */  
  11. 'use strict';  
  12.   
  13. const React = require('React');  
  14. const ReactNative = require('react-native');  
  15. const {  
  16.   KeyboardAvoidingView,  
  17.   Modal,  
  18.   SegmentedControlIOS,  
  19.   StyleSheet,  
  20.   Text,  
  21.   TextInput,  
  22.   TouchableHighlight,  
  23.   View,  
  24.   Dimensions,  
  25.   ScrollView,  
  26. } = ReactNative;  
  27. const {width, height} = Dimensions.get('window');  
  28.   
  29. export default class KeyboardAvoidingViewExample extends React.Component {  
  30.   
  31.   
  32.   constructor(props) {  
  33.     super(props);  
  34.   }  
  35.   
  36.   render() {  
  37.     return (  
  38.       <KeyboardAvoidingView behavior='position' >  
  39.         <ScrollView style={styles.outerContainer}>  
  40.           <View style={styles.container}>  
  41.             <View style={{height:400,backgroundColor:'red',justifyContent:'center',alignItems:'center',}}>  
  42.               <Text style={{fontSize:28,color:'#998462',textAlign:'center',}}>Top Area</Text>  
  43.             </View>  
  44.               <TextInput  
  45.                 placeholder="<TextInput />"  
  46.                 style={styles.textInput} />  
  47.               <View style={{height:200,backgroundColor:'blue',justifyContent:'center',alignItems:'center',}}>  
  48.               <Text style={{fontSize:28,color:'#998462',textAlign:'center',}}>Bottom Area</Text>  
  49.             </View>  
  50.           </View>  
  51.         </ScrollView>  
  52.       </KeyboardAvoidingView>  
  53.     );  
  54.   }  
  55. }  
  56.   
  57. const styles = StyleSheet.create({  
  58.   outerContainer: {  
  59.     height:height,  
  60.     paddingTop: 20,  
  61.   },  
  62.   container: {  
  63.     flex: 1,  
  64.     justifyContent: 'center',  
  65.   },  
  66.   textInput: {  
  67.     borderRadius: 5,  
  68.     borderWidth: 1,  
  69.     height: 44,  
  70.     paddingHorizontal: 10,  
  71.   },  
  72. });  



页面逻辑很简单,模拟表单输入:一个Scrollview,由于表单页面的高度超过了屏幕的高度,所以是带滚动轮的。

里面的内容分3块:上面一个View,中间一个Textinput,下面一个View

页面显示如下:



1、我们先设置KeyboardAvoidingView behavior='padding'

发现设置为padding后并没有解决键盘被覆盖的问题,后来我想是不是因为里面有Scrollview组件的问题,尝试去掉Scrollview组件,将Scrollview改为View就可以了。

可是,我们通常的输入表单都是用Scrollview嵌套的啊???这个问题怎么解?

效果如下:




2、我们再设置KeyboardAvoidingView behavior='position'

 效果如下:



我反复修改Bottom Area 的高度值,发现设置为position时,只是单纯的将当前TextInput的位置上移了 一个键盘的高度。

这样其实也是有问题的,譬如这种情况:


当前输入时,我们的TextInput就已经处于屏幕的顶端,如果再上移一个键盘的高度,那么我的TextInput是不可见的。


3、我们再设置KeyboardAvoidingView behavior='height'

没有任何效果,将Scrollview修改为View,也没有任何效果。


如果大家感兴趣也可以试下,感觉KeyboardAvoidingView的坑很多啊。

如果大家对KeyboardAvoidingView组件的使用有什么好的方法,可以留言告诉我,谢谢!


备注:以上实验是只在iPhone5s 系统 iOS10.1模拟器上进行。

           实验版本:"React-native": "0.35.0"

0 0
原创粉丝点击