在iOS 5中隐藏iPhone键盘

Rob*_*arl 1 iphone uikeyboard ios5

如何查找和删除与内置iPhone键盘对应的视图?

在这个项目中,用户输入的输入UITextField始终是第一个响应者.在以前的iOS版本中(2.1 - > 4,我相信)我们为它添加了一个监听器

[[NSNotificationCenter defaultCenter]
                    addObserver: self
                       selector: @selector(keyboardWillShow:)
                           name: UIKeyboardWillShowNotification
                         object: nil
];
Run Code Online (Sandbox Code Playgroud)

当键盘即将显示时,我们将其删除(我们的文本字段仍为第一响应者).

- (void)keyboardWillShow:(NSNotification *)note {
    UIWindow* tempWindow;
    UIView* keyboard;
    UIView* minusView = nil;
    for(int c = 0; c < [[[UIApplication sharedApplication]
                                            windows] count]; c ++) {
        tempWindow = [[[UIApplication sharedApplication]
                                            windows] objectAtIndex:c];      
        // Loop through all views in the current window
        for(int i = 0; i < [tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            //the keyboard view description always starts with <UIKeyboard
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                minusView = keyboard;
            }
        }
    }
    [minusView removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)

这种方法不再有效,因为在iOS 5中没有UIKeyboard可以通过这种方式找到的类型视图.

但是,有一个视图<UITextEffectsWindow: 0x693cca0; frame = (0 0; 320 480); hidden = YES; opaque = NO; layer = <UIWindowLayer: 0x693cdb0>>,我试图用removeFromSuperviewsetIsHidden方法删除以下描述 .

关于移除键盘和保持第一响应者状态的任何提示?

下面的左侧屏幕截图是加载屏幕,它按预期描绘按钮布局.右侧屏幕显示内置键盘如何遮盖计算器按钮.

应用截图(加载屏幕) 内置键盘遮挡我的按钮

rob*_*off 6

你不应该删除键盘.键盘窗口是私有的,正如您所发现的,可能会在不同版本之间进行更改.

获得所需效果的正确方法是将文本字段的inputView属性设置为包含计算器按钮的视图.然后iOS将负责显示该视图,而不是显示默认键盘.

这在"适用于iOS文本,Web和编辑编程指南"的"数据输入的自定义视图"中进行了介绍.