我该如何更改UISearchBar的搜索文本颜色?

100

我如何改变 UISearchBar 的文本颜色?

25个回答

0

您可以在viewDidLoad中设置self.searchBar.searchTextField.textColor的颜色,以下代码可行。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchBar.searchTextField.textColor = [UIColor whiteColor];
}

0

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{
   
   }
}

0

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

0
在我的情况下,我将UISearchController的searchBar放置在navigationBar中。由于某种原因,只有在将searchController作为navigationBar项目添加后才能调用textColor属性。
previous code...
navigationItem.searchController = searchController
// Only after the assignment it works
searchController.searchBar.searchTextField.textColor = your color

-2

Swift 5 Xcode 11.4 iOS 13.4

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = .white
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = .systemFont(ofSize: 13)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接