Change keyboard color for UISearchBar

I'd love to change the color of the Search button on my keyboard to match the theme of my application. But if it's not possible, at least I would like to do

self.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;

      

But that doesn't work because as I assume searchBar is not a UITextField. So how can I do this successfully? I mean, change the "Search" color on your keyboard: full or just a dark theme.

+3


source to share


4 answers


Try to run



for(UIView *subView in self.searchBar.subviews) {
        if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceDark];
        }
    }

      

+3


source


For iOS 8 use



self.searchBar.keyboardAppearance = UIKeyboardAppearance.Dark

+11


source


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

      

+5


source


Swift 3 update, iOS 10:

    searchBar.keyboardAppearance = .dark
    searchBar.keyboardAppearance = .light
    searchBar.keyboardAppearance = .default
    searchBar.keyboardAppearance = .alert

      

0


source







All Articles