iOS 解决表单被键盘遮住的问题
2016-03-11 14:19
603 查看
问题
处理表单的时候,一定会碰到的就是输入控件被键盘遮住的问题,如图:实例
左边是普通表单,中间是2B表单,右边是文艺表单.
分析
处理这种问题无非就是2个步骤:键盘弹出时,缩小
UITableView的
frame
滚动
UITableView,让当前输入的控件可见
代码写出来就是这几步
捕获键盘事件
计算键盘高度并调整
UITableView的
frame
获取当前正在输入的控件
计算其在
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]; } |
计算键盘高度并调整UITableView
的frame
计算键盘高度并调整UITableView的frame1 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 1314 | - (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]; |
计算其在UITableView
中的位置,并滚动到其位置让其可见
计算其在UITableView中的位置,并滚动到其位置让其可见1 2 3 4 5 6 7 8 9 10 11 12 1314 | - (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]; } |
相关文章推荐
- OC字符串和数组
- WaxPatch在iOS客户端中的一种应用
- IOS开发之格式化日期时间
- 代理传值iOS9与之前版本的区别——提前释放
- iOS 键盘添加工具栏
- iOS开发Xcode7真机调试教程
- iOS开发即时通讯环境搭建-XMPP
- 设置启动图片- Iphone5 上下方黑边,页面显示不全的解决办法
- iOS多线程编程——GCD与NSOperation总结
- IOS版使用移动广告平台集成Google Mobile Ads SDK教程
- iOS多线程详细使用
- 解决想从证书导出p12文件但是该证书中没有密码无法导出P12文件的问题
- 二维码扫描iOS
- IOS使用APNS推送Payload字节数限制导致推送不成功
- iOS点击屏幕使tableview、scrollview回到顶部
- ios 去除按钮的按下效果(阴影)
- iOS广告小科普
- iOS开发之解决隐藏tabbar后原位置无法响应点击事件的问题
- ALAssetsLibrary读取所有照片
- Add Text To Image xamarin.ios