I had been using the following code prior to iOS 11 to customize the appearance of the UISearchController
search bar:
var searchController = UISearc
You need to find the UISearchBar
's underlying UITextField
and change its text color.
Notice this only have effect when search controller is going to present (UISearchControllerDelegate.willPresentSearchController
) or presented.
class ViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// setup your search controller...
// set search controller's delegate
navigationItem.searchController?.delegate = self
}
}
extension ViewController : UISearchControllerDelegate {
func willPresentSearchController(_ searchController: UISearchController) {
// update text color
searchController.searchBar.textField?.textColor = .white
}
}
extension UISearchBar {
var textField: UITextField? {
for subview in subviews.first?.subviews ?? [] {
if let textField = subview as? UITextField {
return textField
}
}
return nil
}
}