217

Note:

See accepted answer (not top voted one) for solution as of iOS 4.3.

This question is about a behavior discovered in the iPad keyboard, where it refuses to be dismissed if shown in a modal dialog with a navigation controller.

Basically, if I present the navigation controller with the following line as below:

navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

The keyboard refuses to be dismissed. If I comment out this line, the keyboard goes away fine.

...

I've got two textFields, username and password; username has a Next button and password has a Done button. The keyboard won't go away if I present this in a modal navigation controller.

WORKS

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
[self.view addSubview:b.view];

DOES NOT WORK

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
UINavigationController *navigationController = 
[[UINavigationController alloc]
 initWithRootViewController:b];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[b release];

If I remove the navigation controller part and present 'b' as a modal view controller by itself, it works. Is the navigation controller the problem?

WORKS

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:b animated:YES];
[b release];

WORKS

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
UINavigationController *navigationController = 
    [[UINavigationController alloc]
         initWithRootViewController:b];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[b release];
5
  • The following SO question seems to be having the same problem, but there are no answers: stackoverflow.com/questions/3019709/…
    – Kalle
    Commented Aug 2, 2010 at 9:51
  • +1 Thank you for your great explanation. But where do I have to put that method? It seems not working where I create the code for presenting the model controller...
    – Lorenzo B
    Commented Jun 26, 2012 at 16:24
  • 1
    It has to be in the modal view controller class itself.
    – Kalle
    Commented Jun 26, 2012 at 17:01
  • Thanks. I see. I solved putting it in a category for UINavigationController class. Cheers.
    – Lorenzo B
    Commented Jun 27, 2012 at 7:23
  • I'm so indebted to you for this question. I was surprised that resignFirstResponder was executing but the keyboard still being shown. My scenario (presentationFormSheet with navig contrllr) is exactly the same as yours. Thanks a ton!! Commented Jan 9, 2013 at 11:23

13 Answers 13

173

This has been classified as "works as intended" by Apple engineers. I filed a bug for this a while back. Their reasoning is that the user is often going to be entering data in a modal form so they are trying to be "helpful" and keep the keyboard visible where ordinarily various transitions within the modal view can cause the keyboard to show/hide repeatedly.

edit: here is the response of an Apple engineer on the developer forums:

Was your view by any chance presented with the UIModalPresentationFormSheet style? To avoid frequent in-and-out animations, the keyboard will sometimes remain on-screen even when there is no first responder. This is not a bug.

This is giving a lot of people problems (myself included) but at the moment there doesn't seem to be a way to work around it.

UPDATE:

In iOS 4.3 and later, you can now implement `-disablesAutomaticKeyboardDismissal' on your view controller to return NO:

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

This fixes the issue.

8
  • 7
    pause Wow, okay. Thanks a lot for the heads up. Damn Apple.. :(
    – Kalle
    Commented Aug 2, 2010 at 10:29
  • Did you submit a bug report to Apple? I have done so under ID# 8384423. I have also submitted a sample application to reproduce the behaviour. Commented Sep 2, 2010 at 2:08
  • 3
    As of iOS 4.3 there is now a disablesAutomaticKeyboardDismissal method which fixes this problem.
    – Kalle
    Commented Mar 31, 2011 at 19:06
  • 5
    i try that disablesAutomaticKeyboardDismissal method, but it still did not solve the problem, how to solve it?
    – R. Dewi
    Commented Sep 21, 2011 at 2:06
  • 3
    @Snips: You need to create a UINavigationController subclass that overrides disablesAutomaticKeyboardDismissal to return NO and use this as the navigation controller when you present a modal form sheet. See the answer from @miha-hribar below.
    – Pascal
    Commented Oct 27, 2012 at 0:20
151

Be careful if you are displaying the modal with a UINavigationController. You then have to set the disablesAutomaticKeyboardDismissal on the navigation controller and not on the view controller. You can easily do this with categories.

File: UINavigationController+KeyboardDismiss.h

#import <Foundation/Foundation.h>

@interface UINavigationController (KeyboardDismiss)

- (BOOL)disablesAutomaticKeyboardDismissal;

@end

File: UINavigationController+KeyboardDismiss.m

#import "UINavigationController+KeyboardDismiss.h"

@implementation UINavigationController(KeyboardDismiss)

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

@end

Do not forget to import the category in the file where you use the UINavigationController.

8
  • 20
    +1, finally I see the missing piece of information for this issue highlighted: that one needs to override disablesAutomaticKeyboardDismissal of UINavigationController, not the own view controller, to fix this issue.
    – DarkDust
    Commented Oct 26, 2011 at 12:55
  • Perfect. Not clear from official docs but makes sense due to the UINavigationController being in the responder chain. Excellent answer. Thank you!
    – imnk
    Commented Apr 17, 2012 at 9:26
  • This bit of knowledge fixed my problem immediately. Thanks!
    – Mark
    Commented May 18, 2012 at 21:20
  • 1
    I'm presenting a modal dialog from a UISplitViewController. I've tried the above code, but substituted UISplitViewController for UINavigationController, but it still doesn't work. Should this method also work on a UISplitViewController?
    – Snips
    Commented Oct 27, 2012 at 14:42
  • 7
    It's not a good idea to implement a duplicate method in a category. You can never be certain which implementation will be called, so at best you can expect inconsistent behavior. Better to inherit from UINavigationController and override the method in your custom class. Commented Apr 18, 2013 at 20:32
118

In the view controller that is presented modally, just override disablesAutomaticKeyboardDismissal to return NO:

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}
5
  • Yep, since 4.3 this seems to be the case. Will update the question. Thanks!
    – Kalle
    Commented Mar 31, 2011 at 19:03
  • 2
    This needs to be added to the navigation controller
    – pottedmeat
    Commented Sep 24, 2013 at 19:50
  • 1
    Yes, works when you overwrite it in the NavigationController. That's the only thing that actually worked for me. Commented Dec 1, 2013 at 3:50
  • Life saver! Why does Apple do stuff like this? Surely it should default to NO & allow us to change it if we really want to
    – SomaMan
    Commented Jun 24, 2014 at 10:27
  • Not working on UIViewController derived class, disablesAutomaticKeyboardDismissal is never called Commented Oct 16, 2015 at 17:54
52

I solved this by using the UIModalPresentationPageSheet presentation style and resizing it immediately after I present it. Like so:

viewController.modalPresentationStyle = UIModalPresentationPageSheet;
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
viewController.view.superview.autoresizingMask = 
    UIViewAutoresizingFlexibleTopMargin | 
    UIViewAutoresizingFlexibleBottomMargin;    
viewController.view.superview.frame = CGRectMake(
    viewController.view.superview.frame.origin.x,
    viewController.view.superview.frame.origin.y,
    540.0f,
    529.0f
);
viewController.view.superview.center = self.view.center;
[viewController release];
6
  • Hmmm...this is not quite right...resizing causes the modal to paint funny...it's like it squishes the content down to fit in the new size box or something...everything looks funny. :(
    – toofah
    Commented Jan 7, 2011 at 16:53
  • There are also rotation problems with this one...if you rotate while this modal is up it will shrink/grow as if it were a full page view
    – toofah
    Commented Jan 7, 2011 at 17:07
  • 3
    toofah, I edited the code to deal with the shrinking/growing problem when rotating; just a matter of giving the superview a flexible top and bottom margin. I'm not sure I'm seeing the other behavior.
    – dvs
    Commented Jan 8, 2011 at 19:53
  • 1
    this works only as long as you don't push a other view on top of this one. Because when you close the view pushed above the UIModalPresentationPageSheet presented view, it's back to it's original size.
    – V1ru8
    Commented Apr 7, 2011 at 15:38
  • It worked. But the word in the view looks a little blur. I don't know why.
    – jeswang
    Commented Aug 23, 2012 at 8:00
1

If you toggle a different modal display you can get the keyboard to disappear. It's not pretty and it doesn't animate down, but you can get it to go away.

It'd be great if there was a fix, but for now this works. You can wedge it in a category on UIViewController and call it when you want the keyboard gone:

@interface _TempUIVC : UIViewController
@end

@implementation _TempUIVC
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
@end

@implementation UIViewController (Helpers)

- (void)_dismissModalViewController {
    [self dismissModalViewControllerAnimated:NO];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [self release];
}

- (void)forceKeyboardDismissUsingModalToggle:(BOOL)animated {
    [self retain];
    _TempUIVC *tuivc = [[_TempUIVC alloc] init];
    tuivc.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentModalViewController:tuivc animated:animated];
    if (animated) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_dismissModalViewController) name:UIKeyboardDidHideNotification object:nil];
    } else
        [self _dismissModalViewController];
    [tuivc release];
}

@end

Be careful with this though as you viewDidAppear / viewDidDisappear and all those methods get called. Like I said, it's not pretty, but does work.

-Adam

1

You could also work around this in a universal app by simply checking the idiom and if it's an iPad, don't pop up the keyboard automatically at all and let the user tap whatever they want to edit.

May not be the nicest solution but it's very straightforward and doesn't need any fancy hacks that will break with the next major iOS release :)

1

Put this code in your viewWillDisappear: method of current controller is another way to fix this:

Class UIKeyboardImpl = NSClassFromString(@"UIKeyboardImpl");
id activeInstance = [UIKeyboardImpl performSelector:@selector(activeInstance)];
[activeInstance performSelector:@selector(dismissKeyboard)];
1

I found that disablesAutomaticKeyboardDismissal and adding a disablesAutomaticKeyboardDismissal function didn't work for my UITextField in a modal dialog.

The onscreen keyboard just wouldn't go away.

My solution was to disable all text-input controls in my dialog, then re-enable the relevant ones a fraction of a second later.

It seems as though when iOS sees that none of the UITextField controls are enabled, then it does get rid of the keyboard.

0

I'm sure you have looked at this, but you are sure that your controller class is properly hooked up as the UITextField delegate, right?

1
  • I set it manually myself, and the delegate methods are called, so yeah.
    – Kalle
    Commented Aug 1, 2010 at 8:35
0

maybe don't return NO, but YES. So it can go away.

And you have a textFieldShouldEndEditing returning YES as well?

And why are you firing [nextResponder becomeFirstResponder]?! sorry i see now

I also have a number of UITextViews which all have their "editable" property set to FALSE.

May we assume none of them, by any chance, has a tag value of secondField.tag+1? If so, you're telling them to become first responder, instead of resigning the first responder. Maybe put some NSLog() in that if structure.

10
  • 1
    NO = don't insert newline, from what I can tell. And setting it to YES didn't fix it.
    – Kalle
    Commented Jul 30, 2010 at 14:36
  • 1
    A UITextField, being one line by definition, doesn't do much with newlines, I think. So it's more about processing pressing of the Return/Done button, as stated in the docs.
    – mvds
    Commented Jul 30, 2010 at 14:51
  • Are you very sure you hooked everything up the right way? Have you put an NSLog("tf %x / method ...",textField); in all delegate functions?
    – mvds
    Commented Jul 30, 2010 at 15:04
  • Well, the delegate functions are called appropriately, and they wouldn't be if the delegate wasn't set up appropriately. And the NSLog gives an EXC_BAD_ACCESS. Also warns me about it being incompatible type in XCode.
    – Kalle
    Commented Aug 1, 2010 at 8:34
  • D'oh. Sorry, should've seen that myself. I've updated the answer above with the results of these NSLogs since formatting will gook itself in comm..
    – Kalle
    Commented Aug 1, 2010 at 10:27
0

For those having trouble with UINavigationController, see my answer to a similar question here: https://stackoverflow.com/a/10507689/321785

Edit: I consider this an improvement to Miha Hribar's solution (since the decision is taking place where it should), and as opposed to Pascal's comment regarding a category on UIViewController

0

may be not a perfect solution ,but works
[self.view endEditing:YES];
from wherever your button or gesture is implemented to present modal

0
Swift 4.1:
extension UINavigationController {
   override open var disablesAutomaticKeyboardDismissal: Bool {
      return false
   }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.