在网上找到的资料比较零散,这部分学起来感觉也有点空虚,内容就只包括隐藏键盘和键盘高度两部分

    隐藏键盘其实就在我学习iOS开发的第一个程序里面已经实践过了,不过当时还懵懵懂懂,现在就了解了是什么一回事,就记录一下,也额外加点内容上去。

    说这个键盘的出现和隐藏是和输入框获取和失去焦点有关系,输入框获取了焦点,软键盘就会出现;输入框失去了焦点,软键盘就会消失。这个就和Android的有出入。所以要键盘消失其实很简单的一行代码就可以完成了



[nameTextField resignFirstResponder]; //nameTextFiled就是输入框的名字



但是在哪个地方执行,触发机制就多加一点步骤了。让键盘消失首要的肯定是文本框输入完毕(以按回车键为准)就应该消失,那就要去实现UITextFieldDelegate的- (BOOL)textFieldShouldReturn:(UITextField *)textField 方法,并且把输入框的delegate设置成当前的ViewController。代码如下



@interface HGTabItem2KeyboardViewController : UIViewController<UITextFieldDelegate>

{

}

@end



 ViewDidLoad的代码



self.txtTextBox.delegate=self;



 添加方法



-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

if(self.txtTextBox==textField)

[textField resignFirstResponder];

return true;

}



在平时使用软键盘的时候总有一个习惯:不是没次打字都想打完结束的,当打字打到一半然后推出不想打让软键盘消失的时候,就会点击一下屏幕空白的地方,软键盘就消失了。现在已经学习了触控事件的话对这个已经不难了,除了在- (BOOL)textFieldShouldReturn:(UITextField *)textField 方法里面调用resignFirstResponder方法外,还在触控的事件里也调用self.view endEditing就行了,这个触控事件可以使用touchesEnded:withEvent:,也可以使用UITapGestureRecognizer 



-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

  [self.view endEditing:true];

}



 声明代码



@interface HGTabItem2KeyboardViewController : UIViewController<UITextFieldDelegate,UIGestureRecognizerDelegate>
{

}

@end



ViewDidLoad中的代码



UITapGestureRecognizer *gestrue=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];

gestrue.numberOfTouchesRequired=1;

[self.view addGestureRecognizer:gestrue];



 添加方法



-(void)singleTap:(UITapGestureRecognizer *)gecognizer
{

  [self.txtTextBox endEditing:true];

}



处理键盘高度实际上是利用了键盘的全局事件,网上有种说法是在iOS5.0以后,键盘会在不同的输入语言下高度会有所变化,但经我在模拟器上实践后发现这个高度的差别不存在,但是这个键盘高度还是有用的,例如QQ,微信在键盘出现的时候,整个视图会往上移动,移动的距离就是键盘高度,这个就用到了键盘高度了。键盘高度是用到键盘的全局事件

  • UIKeyboardWillShowNotification;
  • UIKeyboardDidShowNotification;
  • UIKeyboardWillHideNotification;
  • UIKeyboardDidHideNotification;

在iOS5.0还增加了两个事件

  • UIKeyboardWillChangeFrameNotification
  • UIKeyboardDidChangeFrameNotification

以上的事件看名字就知道是在什么时候触发,但是ChangeFrame事件的效果不算明显,当因为当键盘高度变化的时候同时触发了show和ChangeFrame两个事件,下面还是把代码粘出来

在ViewDidLoad加入以下方法



[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];



再加入以下事件方法



- (void)didReceiveMemoryWarning
{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{

if(self.txtTextBox==textField)

[textField resignFirstResponder];

return true;

}

 

-(void)keyboardWillChange:(NSNotification *)notif
{

NSLog(@"keyboardWillChange");

}

 

-(void)keyboardDidChange:(NSNotification*)notif
{

 

}

 

-(void)keyboardWillShow:(NSNotification*)notif
{
//keyboard height will be 216, on iOS version older than 5.0

CGRect boxFrame = self.view.frame;

boxFrame.origin.y = -[[[notif userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height+

self.tabBarController.tabBar.frame.size.height;//50;

[UIView animateWithDuration:0.3f animations:^{

self.view.frame = boxFrame;

}];

NSLog(@"keyboardWillShow");

}

-(void)keyboardWillHide:(NSNotification*)notif
{

CGRect boxFrame = self.view.frame;

boxFrame.origin.y =0; //216;

[UIView animateWithDuration:0.3f animations:^{

self.view.frame = boxFrame;

}];

}



在点击的时候,文本框会上移,但是文本框刚好在键盘的顶部,还是在视图的最底部。当键盘隐藏时,文本框刚好下移,最终刚好在屏幕最底端。