1.在xib的头文件里面添加textField的委托协议,如:@interface ZLViewController : UIViewController<UITextFieldDelegate>;
2.在xib 的 .m文件里的函数viewDidLoad添加某个或某几个textField的delegate如:self.messageTextField.delegate=self;
3.然后添加相应的函数如下:
//键盘上移
//该方法为点击输入文本框要开始输入是调用的代理方法:就是把view上移到能看见文本框的地方
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGFloat keyboardHeight = 216.0f;
if (self.view.frame.size.height - keyboardHeight <= textField.frame.origin.y + textField.frame.size.height) {
CGFloat y = textField.frame.origin.y - (self.view.frame.size.height - keyboardHeight - textField.frame.size.height - 5);
[UIViewbeginAnimations:@"srcollView"context:nil];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIViewsetAnimationDuration:0.275f];
self.view.frame = CGRectMake(self.view.frame.origin.x, -y, self.view.frame.size.width, self.view.frame.size.height);
[UIViewcommitAnimations];
}
}
//该方法为点击虚拟键盘Return,要调用的代理方法:隐藏虚拟键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
returnYES;
}
//该方法为完成输入后要调用的代理方法:虚拟键盘隐藏后,要恢复到之前的文本框地方
- (void)textFieldDidEndEditing:(UITextField *)textField{
[UIViewbeginAnimations:@"srcollView"context:nil];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIViewsetAnimationDuration:0.275f];
self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIViewcommitAnimations];
}