Change Keyboard Color in UISearchBar with apperance

偶尔善良 提交于 2020-01-01 19:01:12

问题


I want to change the color of the keyboard to black, when the user taps on the search textfield.

I was trying to achieve it with UITextField *textField = [UITextField appearance]; [textField setKeyboardAppearance:UIKeyboardAppearanceAlert];

but my build fails with this message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISearchBarTextField _UIAppearance_setKeyboardAppearance:]: unrecognized selector sent to instance 0x8485260'

Please can you help me with that? Thanks a lot


回答1:


use this code...

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

its same like my another answer in which i replace the button Image see.. image-for-cancel-button-of-uisearchbar




回答2:


Upvote to Paras Joshi's answer, and an update for iOS7/8. The UIView is now wrapped in one more layer, so you'll want to iterate once more.

 for(UIView *searchTextfield in self.searchBar.subviews)
    {
        for(UIView *searchTextfield2 in searchTextfield.subviews) {
            if([searchTextfield2 isKindOfClass: [UITextField class]]){
                [(UITextField *)searchTextfield2 setKeyboardAppearance: UIKeyboardAppearanceDark];
            }
        }


    }

Standard disclaimer. This is "officially" frowned upon by Apple, but because you're simply iterating through UIViews using public api, you're "technically" okay.



来源:https://stackoverflow.com/questions/16275061/change-keyboard-color-in-uisearchbar-with-apperance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!