问题

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

实例

左边是普通表单,中间是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. Web Component--01. 简介

    Web Components 是什么? Web Components是W3C定义的新标准,它给了前端开发者扩展浏览器标签的能力,可以自由的定制组件,更好的进行模块化开发,彻底解放了前端开发者的生产力. ...

  2. sass调试--页面看到sass文件而不是css文件问题

    在浏览器页面有时看到sass文件而不是css文件问题,其主要由于sass开启了source-map(调试)功能,问题如下图: sass调试 sass调试需要开启编译时输出调试信息和浏览器调试功能,两者 ...

  3. How can I learn to program?

    黑客与画家:硅谷创业之父paul graham关于回答‘How can I learn to program’ How can I learn to program? Find a friend wh ...

  4. 敏捷个人微信号:AgileMe ,欢迎大家推广和关注

  5. 邮箱mail 发送类 ASP.NET C#

    没有牛B的设计模式,代码精练精练实用,功能齐全,调用简单 ..全全完完为码农考虑 MailSmtp ms = new MailSmtp("smtp.qq.com","12 ...

  6. IIS Express魔法堂:解除localhost域名的锁定

    一.前言   单点登录是通过域名从cookie中获取登录信息,然后再根据cookie的键值对获取用户信息.但由于通过IIS Express调试应用时默认使用localhost作为域名且无法直接修改,导 ...

  7. Do not to test a private method.

    If you want to unit test a private method, something may be wrong. Unit tests are (generally speakin ...

  8. DataGridView隐藏列用CSS实现

    隐藏DataGridView某一列,用CSS控制 CSS Code: .hidden{ display:none;} c# Code: <asp:BoundField DataField=&qu ...

  9. 内存分段 && 缓冲区 && 析构函数

    一.内存中的程序: 在进程被载入内存中时,基本上被分成许多小的节,以下是6个主要的节. 低地址                                                   高地 ...

  10. 四、MyBatis主配置文件

    //备注:该博客引自:http://limingnihao.iteye.com/blog/1060764 在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码 收藏 ...