• 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

  • 相关阅读:
    小程序中的箭头函数
    总结:小程序常见问题(2)
    总结:小程序常见问题(1)
    实战:云开发-实现奶茶店小程序(二)
    实战:云开发-实现奶茶店小程序(一)
    实战:云开发-实现在线充值小程序
    ORACLE表名与列名小写转成大写
    MSSQL所有表名、列名转大写的SQL语句
    ORACLE 之 标识符无效 问题总结及解决方案
    SQL语句获取数据库中的表主键,自增列,所有列
  • 原文地址:https://www.cnblogs.com/SnailFish/p/2991026.html
Copyright © 2020-2023  润新知