2013年2月18日星期一

iPad keyboard will not dismiss if modal view controller presentation style is UIModalPresentationFormSheet


在ipad上采用 方式 present 一个 navigation controller 时,present后弹出的keyboard,不能够 dismiss 掉。即使 textfield resignFirstResponder,keyboard也不会消失。

Problem:
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:
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.



Solution:
Be careful if you are displaying the modal with a UINavigationController. You then have to set thedisablesAutomaticKeyboardDismissal on the navigation controller and not on the view controller. You can easily do this with categories.
File: UINavigationController+KeyboardDismiss.h
#import 

@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.

没有评论:

发表评论