iOS解决键盘遮挡输入框问题
来源:互联网 发布:linux ftp更改下载目录 编辑:程序博客网 时间:2024/05/19 08:02
导读:UITextField(输入框)获取焦点后会弹出键盘,有时候键盘会遮挡住输入框,影响用户交互,所以需要在弹出键盘的时候将视图上移至不会遮挡的位置。下面主要讲述几种常见解决方法。
一、弹出键盘时,将整个视图上移:监听键盘事件
//监听键盘//1、键盘弹出时[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];//2、键盘收回时[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillhide) name:UIKeyboardWillHideNotification object:nil];/**监听键盘弹出,输入框上移*/-(void)keyboardWillShow{NSDictionary *userInfo = note.userInfo;CGFloat duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];CGRect keyFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];//获取键盘高度CGFloat moveY = keyFrame.origin.y - self.view.frame.size.height-64;[UIView animateWithDuration:duration animations:^{self.view.transform = CGAffineTransformMakeTranslation(0, moveY);}];}/**监听键盘弹收回,输入框下移*/-(void)keyboardWillhide {//设置view的frame,往下平移}
如果你的UIViewController继承UITableViewController,这样系统会自动处理键盘遮挡的问题,如果不是,但又需要使用UITableview,该怎么做呢?
二、如果UITextField放在UITableview上,就要知道每个输入框所在的cell的位置,然后再做处理。
- (void)textFieldDidBeginEditing:(UITextField *)textField {//获取cell的位置UITableViewCell *cell = (UITableViewCell *)[textField superview];NSIndexPath *indexPath = [_publishTableView indexPathForCell:cell];CGRect rectInTableView = [self.publishTableView rectForRowAtIndexPath:indexPath];} CGFloat keyboardHeight = 216.0f; if (tableview.frame.size.height - keyboardHeight <= rectInTableView.origin.y + textField.frame.size.height+THfloat(36)) { CGFloat y = rectInTableView.origin.y + textField.frame.size.height+THfloat(36) - (self.publishTableView.frame.size.height - keyboardHeight-THfloat(44)); [UIView beginAnimations:@"srcollView" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.275f]; tableview.frame = CGRectMake(0, -y, THScreenW, THScreenH-64); [UIView commitAnimations]; }
三、三句代码实现该功能
UITableViewController *tvc = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];tableView = tvc.tableView;[self addChildViewController:tvc];
阅读全文
0 0
- iOS解决键盘遮挡输入框问题
- iOS解决键盘遮挡输入框的问题
- 解决iOS键盘弹出遮挡输入框问题
- iOS解决键盘弹出遮挡输入框问题
- 如何解决键盘遮挡输入框问题
- 解决键盘遮挡输入框问题
- iOS键盘弹出遮挡输入框问题
- iOS解决键盘遮挡问题
- iOS 解决键盘遮挡问题
- iOS解决键盘遮挡问题
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- android,键盘遮挡了输入框,完美解决android键盘遮挡问题
- 解决键盘遮挡输入框(UITextField)问题
- 解决键盘遮挡输入框(UITextField)问题
- 解决键盘遮挡输入框(UITextField)问题
- 简单完美的解决键盘遮挡输入框的问题
- 一招解决全局键盘遮挡输入框问题
- 弹出一个window
- MIME类型是什么?包含哪些类型?
- 共享内存---结构体使用
- linux tomcat java.lang.OutOfMemoryError: unable to create new native thread
- gdb 之 mac下调试
- iOS解决键盘遮挡输入框问题
- 变量更新和控制依赖
- windows+tensorflow+fasterRcnn---1
- 模仿tmall商城网站
- POJ 1094 Sorting It All Out(拓扑排序)
- JavaScript bind()方法介绍
- ----時區----
- python爬虫进阶(五):数据的存储
- LightOJ