键盘的使用
1:键盘打开和关闭通知
//注册键盘关闭打开事件 UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(whenKeyboardShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(whenKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
------------------------------------------------------------------
2:点击键盘reture让键盘消失
//注册键盘按键委托
//<UITextFieldDelegate>
self.textField.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self KeyDismiss];
return true;
}
//让键盘消失
-(void)KeyDismiss{
[self.textField resignFirstResponder];
}
--------------------------------------------------------------------------------------------------------------
3:点击view让键盘消失
//注册view点击后隐藏键盘
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:(self) action:@selector(KeyDismiss)]];
4:键盘出现和隐藏时文本框的和scroll滚动
-(void)whenKeyboardHide:(NSNotification*) notif{
//获取当前键盘高度
NSDictionary* info = notif.userInfo;
NSValue* value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
//重设scrollview高度
[UIView animateWithDuration:0.2f animations:^{
[self.scrollView setContentOffset:CGPointMake(0,self.prevY-keyboardRect.size.height)];
[self.textField setFrame:CGRectMake(0,538,320,30)];
}];
//若没有scrollview可以使用uiview位移
//CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -keyboardRect.size.height/2);
//[_txt_email setTransform:myTransform];
//[_txt_pwd setTransform:myTransform];
}
-(void)whenKeyboardShow:(NSNotification*) notif{
//获取当前键盘高度
NSDictionary* info = notif.userInfo;
NSValue* value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
//重设scrollview高度
[UIView animateWithDuration:0.1f animations:^{
[self.scrollView setContentOffset:CGPointMake(0, keyboardRect.size.height)];
[self.textField setFrame:CGRectMake(0,568-keyboardRect.size.height-30,320,30)];
}];
//若没有scrollview可以使用uiview位移
//CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -keyboardRect.size.height/2);
//[_txt_email setTransform:myTransform];
//[_txt_pwd setTransform:myTransform];
}