如何使 UISearchController SearchBar 背景色成为真实的、无色的颜色?

问题描述 投票:0回答:2

我几乎尝试了所有能找到的改变 UISearchController SearchBar 背景颜色的解决方案,但没有一个能产生正确的背景颜色。每种解决方案都会产生稍深的颜色,如下图所示,白色看起来更苍白/灰白色。

如何让搜索栏变成真正的白色?

导致白色变苍白的最新“解决方案”之一如下:

let searchController = UISearchController()

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.searchController = searchController

    let textFieldInsideSearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
    textFieldInsideSearchBar?.backgroundColor = UIColor.white
}

swift uisearchbar uisearchcontroller
2个回答
0
投票

默认边框样式受到导航项的影响。如果删除边框样式并手动设置圆角半径,它应显示为白色。白色似乎是我遇到的唯一受此影响的颜色。

更改:

 textFieldInsideSearchBar?.backgroundColor = UIColor.white

 textFieldInsideSearchBar?.borderStyle = .none
 textFieldInsideSearchBar?.cornerRadius = 10
 textFieldInsideSearchBar?.backgroundColor = .white

当我将搜索栏放在表头中时,上面的代码(不改变边框/半径)工作没有问题,但是当我尝试将其嵌入到导航栏项目中时,我遇到了你的问题。


0
投票

我遇到了类似的问题,UISearchBar 的

backgroundColor
显得比预期更暗。使用视图调试器,我注意到一个半透明视图,它覆盖了我设置的
backgroundColor
的视图。为了解决这个问题,这是我想出的解决方案:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let textField = searchController.searchBar.searchTextField
    textField.subviews.first?.subviews.first?.removeFromSuperview()
}
© www.soinside.com 2019 - 2024. All rights reserved.