Perjan Duro
Perjan Duro

Reputation: 372

Change Keyboard Color in UISearchBar with apperance

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

Upvotes: 2

Views: 1215

Answers (2)

timothykc
timothykc

Reputation: 2235

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.

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20541

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

Upvotes: 4

Related Questions