1. 添加编辑框:
UITextField *field=[[UITextField alloc]initWithFrame:CGRectMake(100, 350, 200, 50)];
field.delegate=self;
[self.view addSubview:field];
2.编辑时界面上移:
-(void)textFieldDidBeginEditing:(UITextField *)textField //此方法为textField的代理方法,需先设置代理
{
//设置动画的名称
[UIView beginAnimations:@"Animation" context:nil];
//设置动画的间隔时间
[UIView setAnimationDuration:0.20];
//使用当前运行的状态开启下一段动画
[UIView setAnimationBeginsFromCurrentState:YES];
//设置视图的位移
self.view.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-100, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
}
3.编辑完成,还原:
-(void)textFieldDidEndEditing:(UITextField *)textField
{
//设置动画的名字
[UIView beginAnimations:@"Animation" context:nil];
//设置动画的间隔时间
[UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 100, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
}
4.设置点击return收起键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
5.点击界面收起键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//[field resignFirstResponder];//设置textfield全局
[self.view endEditing:YES];
}