关于IOS键盘遮住输入框以及关闭的方法。
2012-12-04 15:11
225 查看
一。在普通的View界面
1.当点击输入框,弹出键盘挡住输入框,可用增加页面高度,调整坐标。其中count = 0的操作看具体需求。
2.关闭键盘,点击背景出发 touchesBegan事件,通过取消输入框第一响应来关闭键盘,并将页面高度和坐标还原。
二。在UITableView 界面
1.首先自定义UITextField。
2.当点击文本框时,注册手势是用来关闭键盘的
3.UITableView调整输入框高度的另一种方法。
1.当点击输入框,弹出键盘挡住输入框,可用增加页面高度,调整坐标。其中count = 0的操作看具体需求。
- (void)textFieldDidBeginEditing:(UITextField *)textField { //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder if(count == 0){ //当出现键盘时,添加frame的高度,已便键盘的使用 NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y -=130; frame.size.height +=130; self.view.frame = frame; [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; count++; } }
2.关闭键盘,点击背景出发 touchesBegan事件,通过取消输入框第一响应来关闭键盘,并将页面高度和坐标还原。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if (![touch.view isKindOfClass:[UITextField class]]) { [self.loginText resignFirstResponder]; [self.pwdText resignFirstResponder]; } if(count != 0){ NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y +=130; frame.size.height -=130; self.view.frame = frame; //self.view移回原位置 [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; count = 0; } }
二。在UITableView 界面
1.首先自定义UITextField。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if(!cell){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]autorelease]; CGFloat side = cell.contentView.bounds.size.height; //构建text UITextField *text = [[UITextField alloc] init]; text.frame =CGRectMake(85, 0, cell.contentView.bounds.size.width - side-60, side); text.tag=2; text.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin; text.delegate =self; //设置键盘位数字键盘 text.keyboardType = UIKeyboardTypeNumberPad; //添加清楚按钮 text.clearButtonMode = UITextFieldViewModeWhileEditing; //设置文本上下方向居中 text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; cell.backgroundColor = [UIColor whiteColor]; [cell.contentView addSubview:text]; } }
2.当点击文本框时,注册手势是用来关闭键盘的
- (void)textFieldDidBeginEditing:(UITextField *)textField { //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder if(count == 0){ //注册手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didtap:)]; tap.numberOfTouchesRequired =1; tap.numberOfTapsRequired =1; tap.cancelsTouchesInView = NO; [self.openView addGestureRecognizer:tap]; [tap release]; //当出现键盘时,添加frame的高度,已便键盘的使用 NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y -=100; frame.size.height +=100; self.view.frame = frame; [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; count++; } }3.关闭键盘,删除手势,将UITableView设置成不可用。
-(void)didtap:(UITapGestureRecognizer *)didpap { if(count != 0){ //删除手势 [self.openView removeGestureRecognizer:didpap]; //讲UITableView设置成不可用 [self.openView endEditing:YES]; NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y +=100; frame.size. height -=100; self.view.frame = frame; //self.view移回原位置 [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; count = 0; } }
3.UITableView调整输入框高度的另一种方法。
-(void) addHight { if(!isFirst) { //congtentoffset 当前区域顶点相对于frame的偏移量 [uitableView setContentOffset:CGPointMake(0,point.y-10) animated:YES]; } } //添加手势,用来关闭tableView键盘 -(void) tap { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didtap:)]; tap.numberOfTouchesRequired =1; tap.numberOfTapsRequired =1; tap.cancelsTouchesInView = NO; [self.uitableView addGestureRecognizer:tap]; [tap release]; if(!isOne){ CGSize frame = self.uitableView.contentSize; frame.height += 250;//这步看具体情况 self.uitableView.contentSize =frame; isOne = YES; } [self addHight]; } //删除手势,在其中做关闭键盘操作 -(void)didtap:(UITapGestureRecognizer *)didpap { [self.uitableView removeGestureRecognizer:didpap]; [self.uitableView endEditing:YES]; if(isOne){ CGSize frame = self.uitableView.contentSize; frame.height -= 250; self.uitableView.contentSize =frame; isOne = NO; } } - (void)textFieldDidBeginEditingNotDelegate:(UITextField *)textField tableView :(UITableView *)tableView { //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder //获得当前文本框坐标 self.uitableView = tableView; UITableViewCell *cell = (UITableViewCell *)[[textField superview]superview]; point = [tableView convertPoint:textField.frame.origin fromView:cell]; [self tap]; }
相关文章推荐
- IOS开发键盘弹出时遮住输入框的问题的一种解决方法。
- ios基础之关闭输入框的几种常见方法集锦
- IOS开发:关于设置UIView的背景为图片的方法以及问题
- iOS开发-关闭/收起键盘方法总结
- 关于解决关闭eclipse之后tomcat无法启动以及找不到javax.servlet的方法
- 打开键盘遮住View的问题解决方法-IOS开发
- 【转】IOS 输入框被键盘遮盖的解决方法
- iOS开发-关闭/收起键盘方法总结
- iOS textView光标位置输入以及删除字符或者emoji表情(自定义键盘的删除方法)
- iOS 输入框随键盘上移以及点击tableView回收键盘
- iOS关闭键盘的方法
- iOS 中关闭键盘方法
- iOS关闭键盘的五种方法(总结的很好的博文)
- IOS开发-关闭/收起键盘方法总结
- iOS_开发经验总结:键盘遮挡输入框时的解决方法
- 整理所有IOS开发关闭键盘方法
- iOS关闭键盘的两种简单方法
- iOS开发>>>UITextField输入框,点击背景任何地方,实现键盘关闭
- iOS上键盘出现时输入框不被覆盖的几种方法分析
- 关于iOS 点击空白处关闭键盘(手势)