问题

处理表单的时候,一定会碰到的就是输入控件被键盘遮住的问题,如图:

实例

左边是普通表单,中间是2B表单,右边是文艺表单.

分析

处理这种问题无非就是2个步骤:

  1. 键盘弹出时,缩小UITableViewframe
  2. 滚动UITableView,让当前输入的控件可见

代码写出来就是这几步

  1. 捕获键盘事件
  2. 计算键盘高度并调整UITableViewframe
  3. 获取当前正在输入的控件
  4. 计算其在UITableView中的位置,并滚动到其位置让其可见

那么如何一步一步的来实现这些步骤呢?

捕获键盘事件

捕获键盘事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardHide:)
name:UIKeyboardWillHideNotification
object:nil]; - (void)actionKeyboardShow:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil]; } - (void)actionKeyboardHide:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
}

计算键盘高度并调整UITableViewframe

计算键盘高度并调整UITableView的frame

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)actionKeyboardShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size
self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height); [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil]; }

获取当前正在输入的控件

这里得说一句,普通程序员一般是这样来获取的

UIView的Category

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (UIView *) getFirstResponder
{
if (self.isFirstResponder) {
return self;
} for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView getFirstResponder];
if (firstResponder != nil) {
return firstResponder;
}
} return nil;
}

虽然没错,但是文艺程序员应该这样来获取

UIResponder的Category

1
2
3
4
5
6
7
8
9
10
11
static __weak id currentFirstResponder;

+(id)currentFirstResponder {
currentFirstResponder = nil;
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil];
return currentFirstResponder;
} -(void)findFirstResponder:(id)sender {
currentFirstResponder = self;
}

同理,有时候我们需要让键盘消失,那么也有三种做法可以选择

1
2
3
4
5
[someView resignFirstResponder];

[self.view endEditing:YES];

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

如何选择呢? It’s up to U.

计算其在UITableView中的位置,并滚动到其位置让其可见

计算其在UITableView中的位置,并滚动到其位置让其可见

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (void)actionKeyboardShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height); UIView *v = [UIResponder currentFirstResponder]; if ( v )
{
while ( ![v isKindOfClass:[UITableViewCell class]]) {
v = v.superview;
} UITableViewCell *cell = (UITableViewCell*)v; [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForRowAtPoint:cell.center] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
} [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil]; }

iOS 解决表单被键盘遮住的问题的更多相关文章

  1. 安卓手机 HTML5 手机页面 输入表单被键盘遮挡住了

    TML5 手机页面 输入表单被键盘遮挡住了 请问 大神 怎么 js 或者 JQ 判断安卓手机软键盘的键盘隐藏键按下去了? 有使用 uexWindow 方法 能判断到确定键 是 13 但是不知道这个键的 ...

  2. ios TextField 不被键盘遮住

    首先放一个scrollView窗口,将Scroll View视图占整个屏幕. 向Scroll View    添加TextField 控件. 首先,ViewController.h  代码如下; #i ...

  3. iOS开发笔记11:表单键盘遮挡、浮点数价格格式化显示、省市区选择器、View Debugging

    1.表单键盘遮挡 应用场景为一个collectionView上有多个textfield.textView供用户填写信息. 之前输入项较少时,采取的方法比较粗暴,didSelectItemAtIndex ...

  4. 从此不再担心键盘遮住输入框OC(一)

    文/Jiar_(简书作者)原文链接:http://www.jianshu.com/p/48993ff982c1著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 新版本在这里:从此不再担心 ...

  5. 从此不再担心键盘遮住输入框OC(

    从此不再担心键盘遮住输入框OC(二) 字数544 阅读1492 评论15 喜欢25 在我发布这篇文章没多久之前,我发布了一篇叫 从此不再担心键盘遮住输入框OC(一)的文章.我在那篇文章中介绍了我的键盘 ...

  6. iOS 开发之 - 关闭键盘 退出键盘 的5种方式

    iOS 开发之 - 关闭键盘 退出键盘 的5种方式   1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5 ...

  7. iOS Android中 h5键盘遮挡输入框的问题和解决方案

    问题发现:在 Android 部分机型 和 iOS部分系统下 键盘会出现遮挡输入框的情况(壳内).问题解决: Android 经过测试,Android 的6.0版本以上均会出现改问题,归根到底是之前的 ...

  8. iOS 解决LaunchScreen中图片加载黑屏问题

    iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...

  9. 【转】Struts2解决表单重复提交问题

    用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此,重复提 ...

随机推荐

  1. 如何将ToolBar 样式设置Title文字水平居中

    以下是我的activity.xml的代码,线性布局.<android.support.v7.widget.Toolbar         android:id="@+id/toolba ...

  2. codeforces B. Friends and Presents(二分+容斥)

    题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值 ...

  3. 说说你所熟知的MSSQL中的substring函数

    说说你所熟知的MSSQL中的substring函数 *:first-child { margin-top: 0 !important; } body>*:last-child { margin- ...

  4. nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容

    1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...

  5. 字符串js编码转换成实体html编码的方法(防范XSS攻击)

    js代码在html页面中转换成实体html编码的方法一: <!DOCTYPE html><html> <head>    <title>js代码转换成实 ...

  6. 分享几个cocos2dx的小游戏

    先上几个自己写的,因为最近要用cocos2dx,所以就边学边开发几个小游戏玩玩,有捕鱼,连连看,还有打地鼠!都不算完整的项目,不过拿来学习还是可以的,或者在基础上再二次开发,扩展自己! 1:捕鱼的 先 ...

  7. [Solution] AOP原理解析及Castle、Autofac、Unity框架使用

    本节目录: AOP介绍 AOP基本原理 AOP框架 Castle Core Castle Windsor Autofac Unity AOP介绍 面向切面编程(Aspect Oriented Prog ...

  8. 关于Chrome Dev Tool

    ★注意点一:函数名,方法名要拼写准确;

  9. [CLR via C#]8. 方法

    一.实例构造器和类(引用类型) 类实例构造器是允许将类型的实例初始化为良好状态的一种特殊的方法. 类实例构造器方法在"方法定义元数据表"中始终叫.ctor(代表constructo ...

  10. csharp: SDK:CAPICOM

    http://www.microsoft.com/zh-cn/download/details.aspx?id=25281 //************************************ ...