9

当键盘出现时,我想设置

keyboardAppearance = UIKeyboardAppearanceAlert

我检查了文档,看起来你只能更改键盘类型。

这可以在不违反任何 Apple 私有 API 的情况下完成吗?

4

4 回答 4

35

如果您想在整个应用程序中执行此操作,我发现最好的方法是在 UITextField 上使用 Appearance。在启动时将其放入您的 AppDelegate 中。

[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];
于 2013-09-30T19:20:43.537 回答
27

这应该这样做:

for(UIView *subView in searchBar.subviews)
    if([subView isKindOfClass: [UITextField class]])
        [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert];

没有找到其他的方法...

于 2010-02-04T05:47:23.223 回答
9

这不再适用于 iOS 7,因为 UISearchBar 视图层次结构已更改。UITextView 现在是第一个子视图的子视图(例如它在searchBar.subviews[0].subviews数组中)。

一种更面向未来的方法是递归地检查整个视图层次结构,并检查UITextInputTraits协议而不是UITextField,因为这是实际声明方法的内容。一个干净的方法是使用类别。首先在 UISearchBar 上创建一个添加此方法的类别:

- (void) setKeyboardAppearence: (UIKeyboardAppearance) appearence {
    [(id<UITextInputTraits>) [self firstSubviewConformingToProtocol: @protocol(UITextInputTraits)] setKeyboardAppearance: appearence];
}

然后在 UIView 上添加一个添加此方法的类别:

- (UIView *) firstSubviewConformingToProtocol: (Protocol *) pro {
    for (UIView *sub in self.subviews)
        if ([sub conformsToProtocol: pro])
            return sub;

    for (UIView *sub in self.subviews) {
        UIView *ret = [sub firstSubviewConformingToProtocol: pro];
        if (ret)
            return ret;
    }

    return nil;
}

您现在可以像设置文本字段一样设置搜索栏上的键盘外观:

[searchBar setKeyboardAppearence: UIKeyboardAppearanceDark];
于 2013-09-27T18:31:06.790 回答
-2

keyboardAppearance 是 UITextInputTraitsProtocol 的一个属性,这意味着该属性是通过 TextField 对象设置的。我不知道警报键盘是什么,从 SDK 来看,它是适合警报的键盘。

以下是您访问该属性的方式:

UITextField *myTextField = [[UITextField alloc] init];
myTextField.keyboardAppearance = UIKeyboardAppearanceAlert;

现在,当用户点击文本字段并显示键盘时,它应该是您想要的。

于 2009-11-20T19:31:09.810 回答