1. 通过设置回收键盘的回车键来回收,在两者的代理中做一下判断
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
2. 通过在键盘视图上方或者导航栏之类的地方加上按钮来进行回收。
3. 通过在输入框之外的视图加入点击事件进行回收。
- (void)setUpKeyboardTapGesture{
self.tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradShow:) name:UIKeyboardWillShowNotification object:Nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradHide:) name:UIKeyboardWillHideNotification object:Nil];
}
- (void)onTap:(id)sender{
[self.textField resignFirstResponder];
}
- (void)onKeyBoradShow:(id)sender{
[self.view addGestureRecognizer:self.tapGesture]; // 添加手势
}
- (void)onKeyBoradHide:(id)sender{
[self.view removeGestureRecognizer:self.tapGesture]; // 移除手势
}
获取键盘高度的方法
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;