我如何改变 UISearchBar
的文本颜色?
您可以在viewDidLoad中设置self.searchBar.searchTextField.textColor的颜色,以下代码可行。
- (void)viewDidLoad {
[super viewDidLoad];
self.searchBar.searchTextField.textColor = [UIColor whiteColor];
}
SwiftUI,Swift 5
在SwiftUI中,
init(){
UITextfiled.appearance().textColor = .white
}
不起作用。但你可以直接使用
.foregroundColor(.white)
修改searchable()的文本颜色的修饰符。
如果您还想更改NavigationView{}中搜索栏的tintColor。您可以初始化视图的init()函数。
init(){
UINavigationBar.appearance().tintColor = .white
}
var body:some View{
NavigationView{
}
}
Obj-C
UITextField *textField = [self.yourSearchbar valueForKey:@"_searchField"];
textField.textColor = [UIColor redColor];
Swift 4.x
let textField = yourSearchbar.value(forKey: "searchField") as? UITextField
textField?.textColor = UIColor.red
previous code...
navigationItem.searchController = searchController
// Only after the assignment it works
searchController.searchBar.searchTextField.textColor = your color
Swift 5 Xcode 11.4 iOS 13.4
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = .white
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = .systemFont(ofSize: 13)