我有一个UISearchBar
added到UITableView
的顶部。
// init search bar
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.tintColor = Config.grayColor()
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.tableView.tableHeaderView = controller.searchBar
// set content offset for table view in order
// that the searchbar is hidden at the beginning
self.tableView.contentOffset = CGPoint(x: 0, y: controller.searchBar.frame.size.height)
return controller
})()
这基本上看起来像预期:
但是当我进入搜索文本域并向下滚动表视图时,它看起来很奇怪。搜索控制器的背景是透明的。
我试图设置barTintColor
和backgroundColor
,但它没有任何影响。
将搜索栏背景颜色设置为白色
searchBar.backgroundColor = UIColor.white
我几乎测试了一切。对我有用的唯一方法是:
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.setBackgroundImage(UIImage(color: .white), for: UIBarPosition(rawValue: 0)!, barMetrics:.default)
这适用于“正常”和“呈现”形式。 UIImage(color: .white)
只是一种创建1x1彩色图像的方法,用于填充背景。你可以找到并实现here
您可以使用此代码。这对我有用:
UITextField.appearance(whenContainedInInstancesOf:
[UISearchBar.self]).backgroundColor = UIColor.RGB(red: 230, green: 230, blue: 230)
UISearchBar
有一个UITextField
属性,你只需改变上面那种颜色。