1.放弃第一响应者
[textField resignFirstResponder];
2.稍暴力一点的
[self.viewendEditing:YES];
再暴力一点的
[[[UIApplicationsharedApplication] keyWindow]endEditing:YES];
3.点击空白区域收起键盘
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(closeKeyboard)];
tapGesture.cancelsTouchesInView =NO;
[self.viewaddGestureRecognizer:tapGesture];
4.如果找不到当前的第一响应者,键盘收不起来,还有一个特别有效的取巧的方式,那就是新建一个
UITextField,用它获取当前焦点,再放弃第一响应者
UITextField *textField = [[UITextFieldalloc] init];
[self.view addSubview:textField];
[textField becomeFirstResponder];
[textField resignFirstResponder];
5.如果键盘在处于编辑状态时,被一个UIAlertView打断了,可能会出现键盘收起,UIAlertView消失后键盘又弹出又自动收起的诡异现象。这是因为UIAlertView的出现打断了键盘收起的动画,UIAlertView消失后系统又自动开始收起键盘的动画。不过这个情况只在iOS9上出现了,解决方式就是用UIAlertController代替UIAlertView,UIAlertView在iOS8的时候就已经被废弃了,apple推荐我们使用UIAlertController.