在iOS 7上,viewDidDisappear后键盘不会消失

在我们的应用程序中,有一种情况是用户在文本框中input内容,然后按下后退button返回到主屏幕。

如果我们在iOS 7上运行它,键盘不会消失,它只是停留在那里。 用户仍然可以浏览应用程序,但所有文本字段都被禁用,这意味着您无法在任何地方input文本。 用户唯一的select是杀死应用程序,并开始新鲜。

我们尝试添加resignFirstResponder消息,但是这没有任何帮助。

涉及的代码很多,我们正在积极研究这个问题。 同时, 有没有人遇到过这个问题,也许有办法让它消失?

当我编译iOS 7的应用程序时,我遇到了和你一样的问题,并做了以下更改:

  1. 确保你在closuresviewController之前添加[textfield resignFirstResponder] ,例如:

     [_passwordInput resignFirstResponder]; [_emailInput resignFirstResponder]; [self performSegueWithIdentifier:@"forgotPassword" sender:self]; 
  2. 只要确保键盘消失,在viewWillDisappear添加[textfield resignFirstResponder]例如:

     - (void) viewWillDisappear:(BOOL)animated { [_passwordInput resignFirstResponder]; [_emailInput resignFirstResponder]; } 
  3. 如果您的viewController是使用UIModalPresentationFormSheet添加到您的viewController只是为了确保文本字段将响应resignFirstResponder

     - (BOOL)disablesAutomaticKeyboardDismissal { return NO; } 

在你的情况下,重写后退button操作,或者只是使用viewWillDisappear来检查用户按下后退button,然后在[super viewWillDisappear]之前调用resignFirstResponder像这样:

 -(void) viewWillDisappear:(BOOL)animated { [_passwordInput resignFirstResponder]; [_emailInput resignFirstResponder]; [super viewWillDisappear:animated]; } 

尝试[self.view resignFirstResponder] ,而不是viewWillDisappear上的[textfield resignFirstResponder]

[textfield resignFirstResponder]应该完成这项工作,但要确保并且不要遍历所有可以使用的textField:

 [self.view endEditing:YES]; 

从文档:

用于使视图或任何子视图是第一响应者辞职(可选强制)。

一般来说,我觉得这很有用

 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; 

你可以在viewWillDisappear:添加它viewWillDisappear:或者viewDidDisappear:

这将隐藏键盘而不参考当前聚焦的文本字段

我只有UITabBarControlleriOS 8.3 )中的UITabBarController有同样的问题。 也许这个解决scheme不是很“好”,有点复杂,但似乎是有效的,希望它也能帮助你。

 @interface ViewController () @property (nonatomic) BOOL needToHideKeyboard; @property (nonatomic, strong) IBOutlet UITextField *txtField; @property (nonatomic, strong) IBOutlet UIScrollView *scrollView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.needToHideKeyboard = NO; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; [self hideKeayboard]; } - (void)hideKeayboard { if (self.needToHideKeyboard) { [self.txtField resignFirstResponder]; } } - (void)keyboardWasShown:(NSNotification *)notification { self.needToHideKeyboard = YES; NSDictionary *info = [notification userInfo]; CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; // Shift scroll view content insets on the keyboard height UIEdgeInsets contentInsets = self.scrollView.contentInset; contentInsets.bottom = keyboardSize.height; self.scrollView.contentInset = contentInsets; } - (void)keyboardWillBeHidden:(NSNotification *)notification { self.needToHideKeyboard = NO; // Reset keyboard content insets UIEdgeInsets contentInsets = self.scrollView.contentInset; contentInsets.bottom = [self.bottomLayoutGuide length]; self.scrollView.contentInset = contentInsets; } @end 

如果你的视图控制器实现了textFieldDidEndEditing ,那么如果视图正在消失,确保你不要设置另一个视图成为第一个响应者。 当您调用resignFirstResponder[self.view endEditing:YES]时,将调用textFieldDidEndEditing

 [self.view endEditing:YES]; 

停止在我的设备iOS9.x上工作

我们也可以在viewWillDisappear方法中做到这一点

 for (UIView *subview in self.view.subviews) { if ([subview canPerformAction:@selector(endEditing:) withSender:nil]) { [subview endEditing:YES]; } if ([subview canResignFirstResponder]) { [subview resignFirstResponder]; } } 

这将循环通过响应者,并辞去响应者的地位。