UITextFieldDelegate
2、实现其方法
//开始编辑输入框的时候,软键盘出现,执行此事件
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"开始编辑输入框的时候,软键盘出现,执行此事件");
CGRect frame = textField.frame;
//int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//软键盘的高度216
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 226);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if (offset > 0) {
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
}
[UIView commitAnimations];
}
//当用户按下return键或者按回车键,keyboard消失
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"当用户按下return键或者按回车键,keyboard消失");
[textField resignFirstResponder];
return YES;
}
//输入框编辑完成以后,将视图恢复到原始状态
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"输入框编辑完成以后,将视图恢复到原始状态");
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
3、使输入框外部UIView设置成UIController。这样上面就可以实现点击事件了,
点击事件如下:
//点击外部view,软键盘隐藏
- (IBAction)hideSoftKeyboard:(id)sender{
NSLog(@"点击输入框外部UIView(其实已经变成拉UIController),软键盘隐藏");
[self.view endEditing:YES];
}
这样就到定拉,哈哈!简单吧,不喜勿盆
源码下载地址:http://download.csdn.net/detail/zhangzhikaixinya/5774233