首页 > 代码库 > iPad keyboard will not dismiss if modal view controller presentation style is UIModalPresentationFor

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

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

Problem:

在iPad keyboard上发现一个问题,当viewController在navigation controller下以模态视图弹出,并且navigationController.modalPresentationStyle = UIModalPresentationFormSheet时,点击done按钮,软键盘不撤销,即使 textfield resignFirstResponder了。

Solution:

当模态视图在 UINavigationControlle下显示,我们需要设置 navigation controller的disablesAutomaticKeyboardDismissal属性,我们可以通过添加类别实现这个。


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

在需要用到的ViewController文件中导入创建的类别即可。