您的位置:首页 > 产品设计 > UI/UE

UITextField防止键盘遮挡以及添加手势操作消失键盘

2013-11-11 15:37 471 查看

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
gesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:gesture];

添加手势

注意添加UITextField协议

#pragma -mark UITextField delegate
//UITextField的协议方法,当开始编辑时监听
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = KDeviceWidth;
float height = KDeviceHeight;
//上移30个单位,按实际情况设置
CGRect rect=CGRectMake(0.0f,-50,width,height);
self.view.frame=rect;
[UIView commitAnimations];
return YES;
}

#pragma -mark View Action
//恢复原始视图位置
-(void)resumeView
{
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = KDeviceWidth;
float height = KDeviceHeight;

float Y = 0.0f;
CGRect rect=CGRectMake(0.0f,Y,width,height);
self.view.frame=rect;
[UIView commitAnimations];
}

#pragma -mark keyboard Action
//隐藏键盘
-(void)hidenKeyboard
{
[_userTextField resignFirstResponder];
[_passwdTextField resignFirstResponder];
[self resumeView];
}

- (void)nextOnKeyboard:(UITextField *)sender
{
if (sender == _userTextField) {
[_passwdTextField becomeFirstResponder];
}else if (sender == _passwdTextField){
[self hidenKeyboard];
[self resumeView];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: