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

解决键盘遮挡输入框(UITextField)问题

2013-02-28 17:03 471 查看
1.实现UITextFiledDelegate中的协议方法

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


2.恢复移动的视图的方法

//恢复原始视图位置
-(void)resumeView
{
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
//如果当前View是父视图,则Y为20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度
float Y = 20.0f;
CGRect rect=CGRectMake(0.0f,Y,width,height);
self.view.frame=rect;
[UIView commitAnimations];
}


3.点击背景隐藏键盘及响应键盘上Return按键的方法

//隐藏键盘的方法
-(void)hidenKeyboard
{
[self.userNameText resignFirstResponder];
[self.passwordText resignFirstResponder];
[self resumeView];
}

//点击键盘上的Return按钮响应的方法
-(IBAction)nextOnKeyboard:(UITextField *)sender
{
if (sender == self.userNameText) {
[self.passwordText becomeFirstResponder];
}else if (sender == self.passwordText){
[self hidenKeyboard];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: