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://stackoverflow.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui

 

转载于:https://www.cnblogs.com/SnailFish/archive/2013/03/30/2991026.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值