Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change keyboard color for UISearchBar

I would really like to change the color of the "Search" button on the keyboard to match the theme of my app. But if that is not possible, at least I would like to do

self.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;

But that does not work because, as I guess, the searchBar is not a UITextField. So how might I do this successful? I mean, change the color of the "Search" on the keyboard: whether fully or just the dark theme.

like image 518
learner Avatar asked Aug 21 '14 19:08

learner


3 Answers

For iOS 8 use

self.searchBar.keyboardAppearance = UIKeyboardAppearance.Dark

like image 106
Frank Gumeta Avatar answered Oct 27 '22 11:10

Frank Gumeta


Try this, it works with IOS 6 , IOS 7 and IOS 8:

[self setKeyboardOnSearchBar:self.searchBar];

And the function:

-(void)setKeyboardOnSearchBar:(UISearchBar *)searchBar
{
    for(UIView *subView in searchBar.subviews) {
        if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField *)subView setKeyboardAppearance:UIKeyboardAppearanceAlert];
            [(UITextField *)subView setReturnKeyType:UIReturnKeySearch];
        } else {
            for(UIView *subSubView in [subView subviews]) {
                if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
                    [(UITextField *)subSubView setReturnKeyType:UIReturnKeySearch];
                    [(UITextField *)subSubView setKeyboardAppearance:UIKeyboardAppearanceAlert];
                }
            }
        }
    }
}
like image 24
Jouhar Avatar answered Oct 27 '22 12:10

Jouhar


Try the following

for(UIView *subView in self.searchBar.subviews) {
        if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceDark];
        }
    }
like image 4
Konsol Labapen Avatar answered Oct 27 '22 13:10

Konsol Labapen