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

iOS TextField 弹出键盘时实现view整体上移下移:防止弹出键盘遮挡输入框

2017-06-27 10:55 651 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息