Modal Dialog Does Not Dismiss Keyboard

21.7k views Asked by At

I am running into an issue where the keyboard does not get dismissed when leaving a UITextField or UITextView in a UIModalPresentationFormSheet. In addition, I've created a large button to serve as the view's background so if the user taps outside the fields it gets triggered. I am using the same code in a regular view controller, and it works as expected. In the modal view controller it does nothing. Any suggestions would be appreciated.

- (BOOL)textFieldShouldReturn:(id)sender {  
 [titleTextField resignFirstResponder];
 return YES;
}

- (BOOL)textViewShouldReturn:(id)sender {  
 [synopsisTextView resignFirstResponder];
 return YES;
}

- (IBAction)textFieldDoneEditing:(id)sender {  
 [sender resignFirstResponder];
} 

- (IBAction)textViewDoneEditing:(id)sender {  
 [sender resignFirstResponder];
} 

- (IBAction)backgroundClick:(id)sender {  
 [titleTextField resignFirstResponder];
 [synopsisTextView resignFirstResponder];
}
7

There are 7 answers

6
aslı On BEST ANSWER

Overriding disablesAutomaticKeyboardDismissal to return NO as below fixed the same problem of mine. You should put this code to your view controller, from which you initiate the keyboard:

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

Also, check this SO question if you want to get a detailed explanation.

0
Vipin Pareek On

i have also facing same problem and also done everything but not thing works then i start thinking and get some result.

but this answer for those who want to dismiss keyboard on textfield click and then open pop up.

so all you need to call text field delegate

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    if textField == self.myTxtFieldName{
        self.view.endEditing(true) // keyboard hide code
        // here you can call your model or pop up code and keyboard will dismiss and your pop up open 
        return false 
    } 
     return true
} 

Sorry if this is not working for you if there is other answer then please edit it Thank you

0
SafeFastExpressive On

If you implement the UITextFieldDelegate protocol you can inadvertently cause this behavior if you do text validation. If your validation codes returns false from textFieldShouldEndEditing when the text is invalid, the field can't relinquish it's firstResponder status and the keyboard will remain on screen in the next view.

More details at UITextField's keyboard won't dismiss. No, really

0
dvs On

I solved this by resizing a UIModalPresentationPageSheet. See my answer here.

6
Chris Trahey On

For those having trouble with UINavigationController, I think there is a better solution than a category on UIViewController. We should change the behavior of UINavigationController to ask its topViewController (in my opinion, this is how all ViewController containers should handle this).

@implementation UINavigationController (DelegateAutomaticDismissKeyboard)
- (BOOL)disablesAutomaticKeyboardDismissal {
    return [self.topViewController disablesAutomaticKeyboardDismissal];
}
3
Kalle On

If you're presenting a modal view with presentation style "form sheet", Apple apparently does not dismiss the keyboard, thinking that they don't want the keyboard to jump in and out where a user will be doing a lot of editing (i.e. "forms"). The fix would be to change presentation style or live with it.

0
Mike Gledhill On

The disablesAutomaticKeyboardDismissal refused to work for me on iOS 7.

But... I managed to solve this issue by simply disabling the UITextFields on the screen.

My solution is described here.

This workaround even works on Modal UIViewControllers.

Yeah... it surprised me aswell !!