【问题标题】:Change Keyboard Color in UISearchBar with apperance使用外观更改搜索栏中的键盘颜色
【发布时间】:2013-04-22 21:46:59
【问题描述】:
当用户点击搜索文本字段时,我想将键盘的颜色更改为黑色。
我试图通过
UITextField *textField = [UITextField appearance];
[textField setKeyboardAppearance:UIKeyboardAppearanceAlert];
但我的构建失败并显示此消息
由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“-[UISearchBarTextField _UIAppearance_setKeyboardAppearance:]:无法识别的选择器发送到实例 0x8485260”
请问您能帮我解决这个问题吗?
非常感谢
【问题讨论】:
标签:
iphone
ios
objective-c
cocoa-touch
ios5
【解决方案1】:
使用此代码...
for(UIView *searchTextfield in yourSearchBar.subviews)
{
if([searchTextfield isKindOfClass: [UITextField class]]){
[(UITextField *)searchTextfield setKeyboardAppearance: UIKeyboardAppearanceAlert];
}
}
它和我的另一个答案一样,我替换了按钮 Image see.. image-for-cancel-button-of-uisearchbar
【解决方案2】:
赞成 Paras Joshi 的回答,以及 iOS7/8 的更新。 UIView 现在被包裹在一层中,因此您需要再次迭代。
for(UIView *searchTextfield in self.searchBar.subviews)
{
for(UIView *searchTextfield2 in searchTextfield.subviews) {
if([searchTextfield2 isKindOfClass: [UITextField class]]){
[(UITextField *)searchTextfield2 setKeyboardAppearance: UIKeyboardAppearanceDark];
}
}
}
标准免责声明。 Apple“官方”不赞成这种做法,但因为您只是使用公共 api 迭代 UIViews,所以“技术上”没问题。