我真的想更改键盘上“搜索”按钮的颜色以匹配我的应用主题。但如果这是不可能的,至少我想做
self.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;
但这不起作用,因为我猜,searchBar不是UITextField。那么我怎么能成功呢?我的意思是,改变键盘上“搜索”的颜色:是完全还是只是黑暗主题。
答案 0 :(得分:13)
对于iOS 8,请使用
self.searchBar.keyboardAppearance = UIKeyboardAppearance.Dark
答案 1 :(得分:5)
试试这个,它适用于IOS 6,IOS 7和IOS 8:
[self setKeyboardOnSearchBar:self.searchBar];
功能:
-(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];
}
}
}
}
}
答案 2 :(得分:4)
尝试以下
for(UIView *subView in self.searchBar.subviews) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
[(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceDark];
}
}
答案 3 :(得分:1)
Swift 4更新,iOS 11+:
searchBar.keyboardAppearance = .dark
searchBar.keyboardAppearance = .light
searchBar.keyboardAppearance = .default
答案 4 :(得分:0)
iOS 5的Swift 5:
如果特定视图(例如SearchBarView)中存在搜索栏,
UITextField.appearance(whenContainedInInstancesOf: [SearchBarView.self]).keyboardAppearance = .dark
仅会导致SearchBarView内的特定搜索栏发生变化。
否则,如果您想对所有搜索栏进行操作,
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).keyboardAppearance = .dark