将 UISearchBar 背景颜色设置为白色

Set UISearchBar Background Color to White

我遇到了将 UISearchBar 的背景色设置为白色的问题。我试图将 bg 设置为白色,但默认的灰色 bg 颜色仍然存在。 我试过这段代码。

searchController?.searchBar.searchTextField.backgroundColor = UIColor.white

我不明白怎么了。 Bcz 当我设置任何颜色如绿色或红色时,它们很容易设置。 请在 Swift 4+ .

中提供答案

在 Appdelegate 中使用以下内容

if #available(iOS 13, *) {
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = . white
}

我们不能将 SearchBar 的背景颜色设置为白色。

正如我们所见,所有 Apple 默认应用程序都没有白色 BG 颜色,

解决方法:我们需要为此定制SearchBar。