1.通过Keyboard获取键盘高度,改变定位的bottom

  缺点:虚拟键盘完全弹起时,才会获取到键盘高度,定位稍有延迟,而且键盘收起时,定位会出现悬空状态,然后再回到底部

import React, { Component } from 'react'
import {
View,
Text,
Button,
Keyboard,
Platform,
TextInput,
StyleSheet,
ScrollView,
} from 'react-native' import rpx from '../util/rpx' export default class Home extends Component {
constructor(props) {
super(props)
this.state = {
keyBoardHeight: 0
}
}
componentWillMount() {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this));
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this));
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
_keyboardDidShow(e) {
this.setState({
keyBoardHeight: e.endCoordinates.height
});
}
_keyboardDidHide() {
this.setState({
keyBoardHeight: 0
});
}
loseFocus() {
this.refs.input.blur()
}
render() {
let { keyBoardHeight } = this.state
return (
<View style={css.container}>
<ScrollView style={{paddingBottom: rpx(100)}}>
<Text>键盘高度: {keyBoardHeight}</Text>
<View style={{backgroundColor: 'red', height: rpx(500)}}></View>
<Button title="收起键盘" style={css.txt} onPress={() => this.loseFocus()}></Button>
<View style={{backgroundColor: 'blue', height: rpx(500)}}></View>
<View style={{backgroundColor: 'green', height: rpx(500)}}></View>
</ScrollView>
<View style={[css.box, Platform.OS == 'ios' && { bottom: keyBoardHeight }]}>
<TextInput
ref="input"
style={css.input}
placeholderTextColor='#999999'
placeholder={'输入代码、名称或简拼'}
underlineColorAndroid="transparent" />
</View>
</View>
)
}
} const css = StyleSheet.create({
container: {
flex: 1
},
input: {
height: rpx(60),
width: '100%',
fontSize: rpx(26),
color: '#333333',
backgroundColor: '#eee',
borderRadius: rpx(60),
paddingHorizontal: rpx(20),
paddingVertical: 0
},
box: {
width: rpx(750),
height: rpx(100),
backgroundColor: '#fff',
paddingHorizontal: rpx(30),
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
txt: {
color: 'red'
}
})

  2.通过KeyboardAvoidingView组件,将页面推送上去,完美解决

import React, { Component } from 'react'
import {
View,
Text,
Button,
Platform,
TextInput,
StyleSheet,
ScrollView,
KeyboardAvoidingView
} from 'react-native' import rpx from '../util/rpx' export default class Home extends Component {
constructor(props) {
super(props)
this.state = {
}
}
loseFocus() {
this.refs.input.blur()
}
render() {
let behavior = Platform.OS == 'ios' ? 'position' : null
return (
<KeyboardAvoidingView style={css.container} behavior={behavior}>
<ScrollView style={{paddingBottom: rpx(100)}}>
<View style={{backgroundColor: 'red', height: rpx(500)}}></View>
<Button title="收起键盘" style={css.txt} onPress={() => this.loseFocus()}></Button>
<View style={{backgroundColor: 'blue', height: rpx(500)}}></View>
<View style={{backgroundColor: 'green', height: rpx(500)}}></View></ScrollView>
<View style={[css.box]}>
<TextInput
ref="input"
style={css.input}
placeholderTextColor='#999999'
placeholder={'输入代码、名称或简拼'}
underlineColorAndroid="transparent" />
</View>
</KeyboardAvoidingView>
)
}
} const css = StyleSheet.create({
container: {
flex: 1
},
input: {
height: rpx(60),
width: '100%',
fontSize: rpx(26),
color: '#333333',
backgroundColor: '#eee',
borderRadius: rpx(60),
paddingHorizontal: rpx(20),
paddingVertical: 0
},
box: {
width: rpx(750),
height: rpx(100),
backgroundColor: '#fff',
paddingHorizontal: rpx(30),
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
txt: {
color: 'red'
}
})

react native输入框定位在底部(虚拟键盘弹起)的更多相关文章

  1. React Native(四)——顶部以及底部导航栏实现方式

    效果图: 一步一步慢慢来: 其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏.无奈又在忙其他事情,导致这些现在才整理出来. 1.顶部导航栏:react-native-scrollable- ...

  2. Swift - 点击输入框外部屏幕关闭虚拟键盘

    我们如果把文本框的Return Key设置成Done,然后在storyboard中将文本框的Did End On Exit事件在代码里进行关联.同时关联代码里调用文本框的resignFirstResp ...

  3. React Native 的组件之底部导航栏 TabBarIOS(一)

    import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, TabBarIOS, } fro ...

  4. apicloud iphoneX底部虚拟键盘遮挡

    1.首先,底部的高不能写死. 2. var footer = $api.byId('footer'); $api.fixTabBar(footer);这句应该写在 footerHeight = $ap ...

  5. 移动端fixed定位在底部,出现键盘后消失

    jq var h=$(window).height(); $(window).resize(function() { if($(window).height()<h){ $('.nav').hi ...

  6. Android适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  7. Android 适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  8. 虚拟键盘,移动web开发的痛

    原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方.如下图: 如果移动端web也想做类似的功能,可以实现吗?  你可能会说着:“不就是放一个position: fixed;的输入框在 ...

  9. React Native 从入门到原理

    React Native 是最近非常火的一个话题,介绍如何利用 React Native 进行开发的文章和书籍多如牛毛,但面向入门水平并介绍它工作原理的文章却寥寥无几. 本文分为两个部分:上半部分用通 ...

随机推荐

  1. knockoutjs data-bind 声明式绑定整理

    一.Visible绑定 1.功能 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 2.示例 <div data-bind="visible: shouldShowMessa ...

  2. 研究windows下SVN备份及还原恢复方案

    windows下SVN备份方案 备份策略 svn备份一般采用三种方式: 1)svnadmin dump  2)svnadmin hotcopy  3)svnsync.  注意,svn备份不宜采用普通的 ...

  3. Spring Cloud Alibaba到底坑不坑?

    之前我发过一篇<说说我为什么看好Spring Cloud Alibaba>,然后这两天有网友给我转了这篇文章<坑爹项目spring-cloud-alibaba,我们也来一个>, ...

  4. Spring异常分析

    异常报错 2019-01-14 10:40:18.427 ERROR 11776 --- [ost-startStop-1] o.s.b.w.e.t.TomcatStarter : Error sta ...

  5. C# 连接/查询Jira

    查询jira数据,如果是前端,可以按照如下格式直接访问,获取数据 http://jira.company.com/rest/api/2/search?jql=project = REM AND res ...

  6. 使用原生php爬取图片并保存到本地

    通过一个简单的例子复习一下几个php函数的用法 用到的函数或知识点 curl 发送网络请求 preg_match 正则匹配 代码 $url = 'http://desk.zol.com.cn/bizh ...

  7. 折腾Java设计模式之单例模式

    博文原址:折腾Java设计模式之单例模式 单例模式 Ensure a class has only one instance, and provide a global point of access ...

  8. 数据库学习(MySQL):JDBC的简单增删改查实现

    本文为原创,转载请注明出处: https://www.cnblogs.com/Tom-shushu/p/9171896.html 这里我们先在数据库建立一个userinfo表: CREATE TABL ...

  9. Linux硬盘文件分析取证(SSH过的IP)

    在线靶场: https://www.mozhe.cn 背景介绍 某运维人员发现服务器最近被一个IP连接过SSH,请找到连接服务器SSH的IP. 实训目标 1.了解Linux备份方式: 2.了解Acce ...

  10. HTML页面转换为Sharepoint母版页(实战)

    分享人:广州华软 极简 一. 前言 SharePoint有母版页.布局页.母版页存放着如头部(顶部菜单.导航),底部等比较通用部分,通常网站只需一套即可:而布局页,则存放着主要内容部分,根据页面需要, ...