22

I'm trying to hide the iPad keyboard from a modal view controller but it doesn't work. I have tried resignFirstResponder but that doesn't have any affect if we are in a modal view controller. I tried resignFirstResponder in a non-modal UINavigationController with the very same UIViewController and the keyboard hides correctly.

Does anyone know how solve this problem?

Thanks.

[Update] it looks like there's something wrong with my code because the resignFirstResponder does work (I made a simple test case instead of using my code). But I still don't know what the problem is.

Cal
  • 1,625
  • 4
  • 20
  • 30
  • 1
    well I get the same problem in the iPad simulator so you don't need one :) – Cal May 04 '10 at 01:39
  • man, i was wasting a few hours with the same problem – CVertex May 13 '10 at 06:46
  • 1
    Yes this is definitely a bug with iOS 3.2. I fixed it by changing to loginForm.modalPresentationStyle = UIModalPresentationPageSheet instead of UIModalPresentationFormSheet – Eamonn Fallon Jul 16 '10 at 13:33
  • Well, we're now upto iOS 7.1 and XCode 5.1... and this issue is still alive and kicking. And no, changing the "modalPresentationStyle" didn't work for me. (Depressed sigh.) – Mike Gledhill Apr 08 '14 at 09:47

5 Answers5

34

Apparently, there is a new -[UIViewController disablesAutomaticKeyboardDismissal] method that you may override to solve this problem in iOS 4.3.

0xced
  • 25,219
  • 10
  • 103
  • 255
  • 1
    I've implemented this method on my modal UIViewController but it still keeps the keyboard open. Any ideas? Thanks. – morais Mar 10 '11 at 09:46
  • 23
    @morais If you are presenting your view controller inside navigationController you have to subclass UINavigationController and implement the method there. – manicaesar Oct 06 '11 at 11:18
  • Wow ... where is THAT in the documentation. You just saved me a TON of work manicaesar. Thanks! – Jeremy White Apr 06 '12 at 03:38
  • 2
    Holy crap, manicaesar, that needs to be chiseled in the great hall of "Completely undocumented crap that causes developers to tear their hair out" for all to see. Thanks! – Morgan Harris May 02 '12 at 05:25
  • @manicaesar I would assume (though I haven't tested) that the more general rule is that you always have to implement this method in the view controller that's actually being presented, and not in any of its children. So if you're doing custom view controller containment within a modal, you'd override this in the view controller at the root of the modal's view controller hierarchy. – Mark Amery Sep 25 '13 at 18:09
  • @MarkAmery - yes, you are right, that's the message of the ansmwer being commented ;) – manicaesar Sep 26 '13 at 07:16
14

It was because I was using UIModalPresentationFormSheet. All of the other ones work as expected.... Wasted several hours on that.

Cal
  • 1,625
  • 4
  • 20
  • 30
  • I've been having the exact same problem - is this a bug with UIModalPresentationFormSheet? – davbryn May 07 '10 at 15:43
  • I guess they assume if you're doing form entry you will never want to hide the keyboard... I ended up changing it to a non-modal view controller in my case. – Cal May 07 '10 at 18:58
5

This was a total pain to find. Seems like one of the poorer API designs in iOS. Much appreciation to @0xced and @manicaesar for the answers.

Here's my consolidated answer for future devs who are stuck beating their head against the wall.

If it's a single view controller, just override disablesAutomaticKeyboardDismissal and return NO.

If it's a navigation controller in a modal, create your own UINavigationController subclass like so:

In .h...


@interface MyNavigationController : UINavigationController

@end

In .m....

@implementation MyNavigationController


#pragma mark -
#pragma mark UIViewController
- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

@end

In your code that shows a modal view controller.

UIViewController *someViewController = [[UIViewController alloc] init];

MyNavigationController *navController = [[MyNavigationController alloc] initWithRootViewController:someViewController];

navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navController animated:YES];

Jared Egan
  • 1,268
  • 1
  • 12
  • 19
3

I just confirmed the problem is indeed UIModalPresentationFormSheet and filed a bug report to apple rdar://8084017

Alej
  • 126
  • 5
3

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

Community
  • 1
  • 1
dvs
  • 12,324
  • 6
  • 38
  • 45