UIModalPresentationFormSheet风格下的键盘隐藏

本文探讨了在iPad设备上使用UIModalPresentationFormSheet模式显示视图时,键盘无法通过resignFirstResponder关闭的问题,并提供了两种解决方案:一种是修改UIViewController中的disablesAutomaticKeyboardDismissal方法返回值为NO,另一种是在UINavigationController中通过Category扩展重写此方法。这两种方法均解决了键盘无法自动关闭的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值