TextField点击输入时,弹出键盘会遮挡TextField本身,所以也就遮挡了输入框。网上有把TextField控件向上移动的(看这里),但这不适合我的项目,网上找到将整个View向上移的方案,真是我想要的。
首先创建一个textFiled 并实现其代理方法。
- (void)textFieldDidBeginEditing:(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 - 200, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
}
- (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 +200, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[_text resignFirstResponder];
return YES;
}
原文链接: http://www.cnblogs.com/zhanghuanan/p/5611675.html