更改UISearchBar的键盘颜色

lea*_*ner 4 keyboard colors ios xcode5

我真的想改变键盘上"搜索"按钮的颜色,以匹配我的应用程序的主题.但如果这是不可能的,至少我想这样做

self.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为我猜,searchBar不是UITextField.那么我怎么能成功呢?我的意思是,改变键盘上"搜索"的颜色:是完全还是只是黑暗主题.

小智 14

适用于iOS 8使用

self.searchBar.keyboardAppearance = UIKeyboardAppearance.Dark


Jou*_*har 5

试试这个,它适用于IOS 6,IOS 7和IOS 8:

[self setKeyboardOnSearchBar:self.searchBar];
Run Code Online (Sandbox Code Playgroud)

功能:

-(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];
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Kon*_*pen 3

尝试以下操作

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