UISearchBar minimal style has transparent background.

I added a UISearchBar to the top of the 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 })() 

It basically looks like this:

enter image description here

But when I enter the search text box and scroll down the table view, it looks weird. The background of the search controller is transparent.

enter image description here

I tried setting barTintColor and backgroundColor , but this has no effect.

+7
source share
3 answers

set the background searchBar backgroundColor to white

searchBar.backgroundColor = UIColor.white

+2
source

I tested almost everything. The only way that worked for me:

 searchController.searchBar.searchBarStyle = .minimal searchController.searchBar.setBackgroundImage(UIImage(color: .white), for: UIBarPosition(rawValue: 0)!, barMetrics:.default) 

This works in both “normal” and “submitted” forms. UIImage(color: .white) is just a way to create a 1x1 color image that will be used to fill the background. You can find and implement here

0
source

You can use this code. This works for me:

 UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.RGB(red: 230, green: 230, blue: 230) 

UISearchBar has a UITextField property and you just change this color as above.

0
source

All Articles