一般来说,键盘遮挡主要有这么几种情况,一个是遮住UITextView,还有就是遮住UITextField,一般来说,比较推荐在UIScrollView或者UITableView里加入textfield的控件。但是有时也许难免。。

在UITextView中

这个在苹果官方文档中的项目中给出了做法,首先是注册观察者监听UIKeyboardWillShow和WillHide事件

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}

实现自定义的方法


- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo]; CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect newFrame = self.view.frame;
newFrame.size.height -= keyboardRect.size.height; [UIView beginAnimations:@"ResizeTextView" context:nil];
[UIView setAnimationDuration:animationDuration]; self.view.frame = newFrame; [UIView commitAnimations];
}

获取键盘显示的信息,然后根据信息对view的frame进行调整,然后WillHide方法就跟上面相同,只不过把高度新高度改成+= keyboardRect.size.height就可以了,最后,移除观察者:


- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
}

在UIView中遮挡UITextField

大体来说,处理方法同上面类似,主要采取动画平移的方法,而且还能适应切换输入法,我上来定义了个成员来记住一开始UIView的位置,然后在动画的处理上稍微进行了处理。_viewRect = self.view.frame

- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo]; CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect oldFrame = _viewRect; [UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration]; self.view.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y - keyboardRect.size.height + 20, oldFrame.size.width, oldFrame.size.height); [UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect newFrame = _viewRect;
newFrame.origin.y += 20; [UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration]; self.view.frame = newFrame; [UIView commitAnimations];
}

UITableView

UITableView则有些尴尬,因为可以不用代码就可以实现向上平移,但是有一个问题就是点击空白处不能让键盘消失,它也不能去响应UIControl,看了很多方法,但是貌似都或多或少有点问题,这里也请教一下大神们有什么建议。我的方法是从GitHub上找到了一个TPKeyboardAvoidingTableView,让自己的UITableView子类去继承它,就可以很轻松的实现点击消失键盘事件了,具体的源码可以去下载看一看。

解决Keyboard遮盖输入的几种办法的更多相关文章

  1. cocos2d-x解决中文乱码问题的几种办法

    昨天改写cocos2d-x的例程,想在其基础上加上一个计分系统.没有分数实在让人没有玩下去的动力! 我在主场景上加上了一个CCLabelTTF,用于显示分数. 但是意外的发现,当内容含有中文时,CCL ...

  2. Cocos2d-x 3.1.1 学习日志4--cocos2d-x解决中文乱码问题的几种办法

    做个打飞机的游戏,由于版本号太新,网上基本没有教教程,我的版本号是cocos2d-x 3.1.1的.今天遇到cocos2dx中中文乱码的问题.无奈仅仅好Google百度寻求答案,明确了这个问题的缘由. ...

  3. hadoop job解决大数据量关联时数据倾斜的一种办法

    转自:http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2161929.html http://www.geminikwok.com/2011/04 ...

  4. [RN] React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法

    React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法 解决办法: 打开android工程,在AndroidManifest.xml中配置如下: <ac ...

  5. eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样

    eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样 第一种说法: 还真的找到解决的方法了, ...

  6. js检测数据类型四种办法

    面试题中经常会考js数据类型检测,今天我来分享一下js中常用的四种方法判断数据类型,欢迎指点更正. 废话不多说,直入正题. 1.typeof console.log(typeof "&quo ...

  7. oracle导出序列的几种办法

    oracle导出序列的几种办法 注:本文来源于<oracle导出序列的几种办法> 方法一: select 'create sequence ' ||sequence_name|| ' mi ...

  8. android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)

    在个人学习的情况下可能很少使用自定义布局去实现大量复用的情况下,但是在一个开发工作的环境下就会使用到大量复用的自定义控件. 实现思维: 1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想 ...

  9. Atitit 解决Unhandled event loop exception错误的办法

    Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...

随机推荐

  1. 21.python中的闭包和装饰器

    python中的闭包从表现形式上定义(解释)为:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure). 以下说明主要针对 python ...

  2. MySQL Replication(Master与Slave基本原理及配置)

    MySQL Replication(Master与Slave基本原理及配置) 1.  主从mysql server的工作原理:(如图及其过程分析) 过程:   Mysql的复制(replication ...

  3. 【GDKOI 2016】地图 map 类插头DP

    Description 对于一个n*m的地图,每个格子有五种可能:平地,障碍物,出口,入口和神器.一个有效的地图必须满足下列条件: 1.入口,出口和神器都有且仅出现一次,并且不在同一个格子内. 2.入 ...

  4. CSS选择符、属性继承、优先级算法以及CSS3新增伪类、新特性

    CSS 选择符有哪些?哪些属性可以继承?优先级算法如何计算? CSS3新增伪类有那些?CSS新增了哪些特性?下面我整理了一些,仅供参考. CSS 选择符: 1)      id选择器(# myid) ...

  5. tomcat配置问题:访问http://localhost:8080/ 遇到 Access Error: 404

    win7: 8080端口已经被其他应用使用,比如nixxxxxxxxxxxxx When I had an error Access Error: 404 -- Not Found I fixed i ...

  6. STM32 Timer : Base Timer, Input Capture, PWM, Output Compare

    http://www.cs.indiana.edu/~geobrown/book.pdf An example of a basic timer is illustrated in Figure 10 ...

  7. 《大话设计模式》C#/C++版pdf/源码下载

    大话设计模式(带目录完整版)[中文PDF+源代码].zip 下载地址:http://pan.baidu.com/s/1giQP4大话设计模式C++.pdf下载地址:http://pan.baidu.c ...

  8. ansible经常使用模块使用方法

    ansible 默认提供了非常多模块来供我们使用. 在 Linux 中,我们能够通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s ...

  9. ASP.NET Web API中通过ETag实现缓存

    通常情况下Server是无状态的,在ASP.NET Web API中,我们可以让服务端响应体中产生ETag属性,起到缓存的作用.大致实现原理是: 1.服务端的响应体中返回一个ETag属性2.客户端通过 ...

  10. mormot中间件成功匹配客户端FDMemTable和ClientDataSet

    mormot中间件成功匹配客户端FDMemTable和ClientDataSet