Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController iOS 11 Customization

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

var searchController = UISearchController(searchResultsController: nil) searchController.searchBar.setDefaultSearchBar() searchController.searchResultsUpdater = self  if #available(iOS 11.0, *) {     navigationItem.searchController = searchController } else {     tableView.tableHeaderView = searchController.searchBar }  extension UISearchBar {     func setDefaultSearchBar() {         self.tintColor = UIColor.blue         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     } } 

However, the appearance of the search bar fails to update when running the same code on iOS 11.

iOS 10:

enter image description here

iOS 11:

enter image description here

Much of the attention to this question so far has focused on the text color of the search bar. I am looking at more than this - the background color, tint color, the search indicator, clear button color, etc.

like image 213
Alexander MacLeod Avatar asked Aug 13 '17 17:08

Alexander MacLeod


1 Answers

I just found out how to set them: (with some help of Brandon and Krunal, thanks!)

The "Cancel" text:

searchController.searchBar.tintColor = .white 

The search icon:

searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal) 

The clear icon:

searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal) 

The search text:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white] 

enter image description here

The placeholder:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]) 

enter image description here

The white background:

if #available(iOS 11.0, *) {     let sc = UISearchController(searchResultsController: nil)     sc.delegate = self     let scb = sc.searchBar     scb.tintColor = UIColor.white     scb.barTintColor = UIColor.white      if let textfield = scb.value(forKey: "searchField") as? UITextField {         textfield.textColor = UIColor.blue         if let backgroundview = textfield.subviews.first {              // Background color             backgroundview.backgroundColor = UIColor.white              // Rounded corner             backgroundview.layer.cornerRadius = 10;             backgroundview.clipsToBounds = true;         }     }      if let navigationbar = self.navigationController?.navigationBar {         navigationbar.barTintColor = UIColor.blue     }     navigationItem.searchController = sc     navigationItem.hidesSearchBarWhenScrolling = false } 

enter image description here

Taken from here.

like image 115
Darko Avatar answered Sep 21 '22 16:09

Darko