How to HIDE the iPad keyboard from a MODAL view co

2019-01-22 18:54发布

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.

5条回答
老娘就宠你
2楼-- · 2019-01-22 19:35

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

查看更多
可以哭但决不认输i
3楼-- · 2019-01-22 19:39

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

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-22 19:40

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

查看更多
混吃等死
5楼-- · 2019-01-22 19:43

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];

查看更多
狗以群分
6楼-- · 2019-01-22 19:48

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

查看更多
登录 后发表回答