UISearchController iOS 11 Customization

后端 未结 9 1177
南旧
南旧 2021-01-29 21:40

I had been using the following code prior to iOS 11 to customize the appearance of the UISearchController search bar:

var searchController = UISearc         


        
9条回答
  •  醉梦人生
    2021-01-29 22:22

    Try it: searchController..barStyle = .blackOpaque instead of self.searchBarStyle = .minimal.

    Thus:

    var searchController = UISearchController(searchResultsController: nil)
    searchController.searchBar.setDefaultSearchBar()
    //Add this line below
    searchController.searchBar.barStyle = .blackOpaque
    searchController.searchResultsUpdater = self
    
    if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }
    
    extension UISearchBar {
        func setDefaultSearchBar() {
            self.tintColor = UIColor.blue
            //Delete this line below
            self.searchBarStyle = .minimal
            self.backgroundImage = UIImage(color: UIColor.clear)
            let searchBarTextField = self.value(forKey: "searchField") as! UITextField
            searchBarTextField.textColor = UIColor.white
            searchBarTextField.tintColor = UIColor.blue
            searchBarTextField = .dark
        }
    }
    

提交回复
热议问题