您的位置:首页 > 移动开发 > IOS开发

关于IOS键盘遮住输入框以及关闭的方法。

2012-12-04 15:11 225 查看
一。在普通的View界面

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: