iOS UIModalPresentationFormSheet风格下的键盘隐藏

1. 在UIModalPresentationFormSheet(iPad device, without a UINavigationController)下的视图中,如果使用

[inputView resignFirstResponder];

是不能把Keyboard收起的,需要使用以下的方式:

A:

@try

    {

        Class UIKeyboardImpl = NSClassFromString(@"UIKeyboardImpl");

        id activeInstance = [UIKeyboardImpl performSelector:@selector(activeInstance)];

        [activeInstance performSelector:@selector(dismissKeyboard)];

    }

    @catch (NSException *exception)

    {

        NSLog(@"Exception : %@", exception);

    }

上面的方法由于用到了Apple的Private API, 将无法通过审核。

B. 之所以在UIModalPresentationFormSheet下的视图无法用resignFirstResponder是因为在进入到此模式的后,系统将下列中方法返回值置为了YES:

- (BOOL)disablesAutomaticKeyboardDismissal

{

return YES;

}// disablesAutomaticKeyboardDismissal, 此方法是在UIViewController中。

将返回值改为NO后,即可正常使用resignFirstResponsder方法隐藏键盘,但在UINavigationController中此方式依然失效,以下将补充说明此情况。

2. 如果你显示的UIModalPresentationFormSheet视图是在UINavigationController, 那么在UINavigationController的rootViewController中重写

disablesAutomaticKeyboardDismissal方法也将无法启用resignFirstResponder方法效果,因为显示的视图是UINavigationController的,应该在UINavigationController中

来重写disablesAutomaticKeyboardDismissal方法。这里通过将扩展的方式来达到在UINavigationController中重写此方法的目的:

#import <UIKit/UIKit.h>

@interface UINavigationController (UINavigationController_KeyboardDismiss)

- (BOOL)disablesAutomaticKeyboardDismissal;

@end

#import "UINavigationController+UINavigationController_KeyboardDismiss.h"

@implementation UINavigationController (UINavigationController_KeyboardDismiss)

- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}// disablesAutomaticKeyboardDismissal @end

经过UINavigationController Category对disablesAutomaticKeyboardDismissal方法的重写后,即可解决resignFirstResponder方法失效的问题。

参考:http://*.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui

上一篇:WPFの三种方式实现快捷键


下一篇:IOS团队开发之——CocoaPods 第三方库管理工具