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

躍然發表於2014-08-11

在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檔案中匯入建立的類別即可。

相關文章