在 iOS 5 中隐藏 iPhone 键盘

发布于 2025-01-07 03:54:40 字数 1858 浏览 0 评论 0 原文

如何找到并删除 iPhone 内置键盘对应的视图?

在此项目中,用户将输入输入到始终是第一响应者的 UITextField 中。 在以前版本的 iOS(2.1 --> 4,我相信)中,我们添加了一个侦听器,用于

[[NSNotificationCenter defaultCenter]
                    addObserver: self
                       selector: @selector(keyboardWillShow:)
                           name: UIKeyboardWillShowNotification
                         object: nil
];

当键盘即将显示时,我们将其删除(并且我们的文本字段仍然是第一响应者)。

- (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];
}

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

然而,有一个视图的描述如下 >> 我尝试使用 removeFromSuperviewsetIsHidden 方法删除它。

关于移除键盘和保持急救人员状态有什么建议吗?

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

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

How do I find and remove the view corresponding to the built-in iPhone keyboard?

In this project, the user enters input into a UITextField that's always the first responder.
In previous versions of iOS (2.1 --> 4, I believe) we added a listener for the

[[NSNotificationCenter defaultCenter]
                    addObserver: self
                       selector: @selector(keyboardWillShow:)
                           name: UIKeyboardWillShowNotification
                         object: nil
];

When the keyboard is about to show, we then remove it (and our textfield remains first responder).

- (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];
}

This approach no longer works, because in iOS 5 there isn't a view of type UIKeyboard that can be found this way.

However, there is a view with the following description <UITextEffectsWindow: 0x693cca0; frame = (0 0; 320 480); hidden = YES; opaque = NO; layer = <UIWindowLayer: 0x693cdb0>> which I have tried to remove with the removeFromSuperview and setIsHidden methods.

Any tips on removing the keyboard and keeping first responder status?

The left screenshot below is the loading screen, which depicts the buttons layout as intended. The right screencap shows how the built-in keyboard obscures the calculator buttons.

Screenshot of app (loading screen)
Built-in keyboard obscuring my buttons

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭水袖舞倾城 2025-01-14 03:54:40

您不应该卸下键盘。键盘窗口是私有的,并且正如您所发现的,可能会在版本之间发生变化。

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

这记录在 “数据输入的自定义视图”对于 iOS。

You should not be removing the keyboard. The keyboard window is private and, as you have discovered, likely to change between releases.

The correct way to get the effect you want is to set your text field's inputView property to the view that contains your calculator buttons. Then iOS will take care of showing that view for you, instead of showing the default keyboard.

This is documented in “Custom Views for Data Input” in the Text, Web, and Editing Programming Guide for iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文