使用外观更改UISearchBar中的键盘颜色

时间:2013-04-29 09:16:56

标签: iphone ios objective-c cocoa-touch ios5

当用户点按搜索文本字段时,我想将键盘的颜色更改为黑色。

我试图用它来实现它 UITextField *textField = [UITextField appearance]; [textField setKeyboardAppearance:UIKeyboardAppearanceAlert];

但我的构建失败并显示此消息

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UISearchBarTextField _UIAppearance_setKeyboardAppearance:]:无法识别的选择器发送到实例0x8485260'

请你能帮助我吗? 非常感谢

2 个答案:

答案 0 :(得分:4)

使用此代码...

    for(UIView *searchTextfield in yourSearchBar.subviews)
    {
        if([searchTextfield isKindOfClass: [UITextField class]]){
            [(UITextField *)searchTextfield setKeyboardAppearance: UIKeyboardAppearanceAlert];
        }
    }

就像我的另一个答案一样,我替换了按钮图像,请参阅.. image-for-cancel-button-of-uisearchbar

答案 1 :(得分:1)

赞成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];
            }
        }


    }

标准免责声明。这是苹果公司“正式”不赞同的,但是因为你只是使用公共api迭代UIViews,你在技术上“没问题”。