如何为整个应用程序设置UIKeyboard样式?

4

我目前正在尝试全局设置键盘样式,但我的方法不起作用。 我尝试使用UIAppearance并将这行代码放在AppDelegate中:

[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceAlert];

但我在控制台上得到的是以下内容:

[UISearchBarTextField _UIAppearance_setKeyboardAppearance:]: unrecognized selector sent to instance 0xa1897e0

有什么解决方法吗?
2个回答

8

iOS7扩展了UIAppearance协议,因此您现在可以在UITextField上设置keyboardAppearance。

[UITextField appearance].keyboardAppearance = UIKeyboardAppearanceDark;

然而,UITextView不支持这个功能。对于这个类,我会选择jszumski的解决方案。


我最近尝试了一下,但它不能在iOS 8.2中运行。至少,在与表视图一起使用时是这样的。 - bhr
是的!这可能会导致崩溃,但您仍然可以直接设置到TextView上:textView.keyboardAppearance = UIKeyboardAppearanceDark; - yuhua
似乎没有一种方法可以与UIKeyboardTypeNumberPad配合使用。 - Bill Cheswick

2

UIAppearance 只能与明确标记为 UI_APPEARANCE_SELECTOR 的方法兼容,而不幸的是,keyboardAppearance 不属于这些方法之一。

获得所需效果的最佳方式是子类化 UITextField 并在子类的 initinitWithFrame: 方法中设置 keyboardAppearance = UIKeyboardAppearanceAlert; 然后只在应用程序中使用您的类,而不是 UITextField


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接