uitextfield键盘遮挡问题

来源:互联网 发布:淘宝能办理信用卡 编辑:程序博客网 时间:2024/04/29 04:19

转载自:http://hi.baidu.com/xuen2460697/item/17e04807bed6f2873d42e265

键盘遮挡无非是要上移view,然后还原.

1
2
3
4
5
6
7
8
9
10
11
12
13
//键盘出现时,view上移
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
self.view.frame = CGRectMake(0, -130, 320, 460);//130就是上移的距离
[UIView commitAnimations];
     
//键盘消失后,view还原
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
self.view.frame = CGRectMake(0, 0, 320, 460);//还原
[UIView commitAnimations];

下面附上我的项目中当用uitextfield时处理键盘遮挡问题:

//chenyong textfield委托方法,当按下Return时键盘收起

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIViewsetAnimationDuration:0.3];

    [UIViewsetAnimationDelegate:self];

    self.view.frame =CGRectMake(0, 0,320, 460);//还原

    [UIViewcommitAnimations];

    

    [textField resignFirstResponder];

    return YES;

}

//-(BOOL)textFieldShouldEndEditing:(UITextField *)textField

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIViewsetAnimationDuration:0.3];

    [UIViewsetAnimationDelegate:self];

    self.view.frame =CGRectMake(0, -130,320, 460);//130就是上移的距离

    [UIViewcommitAnimations];

    

    return YES;

}