Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar minimal style has transparent background

I've a UISearchBaradded to the top of an 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
    })()

This basically looks like expected:

enter image description here

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

enter image description here

I've tried to set barTintColor and backgroundColor but it doesn't have any effect.

like image 545
Chris Avatar asked Feb 01 '16 17:02

Chris


2 Answers

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

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

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

like image 159
GuillermoMP Avatar answered Nov 09 '22 22:11

GuillermoMP


set the searchBar's backgroundColor to white

searchBar.backgroundColor = UIColor.white

like image 2
Dan Moore Avatar answered Nov 09 '22 21:11

Dan Moore