How to change the default gray background color of the search text field in UISearchController?

enter image description here

    2 answers 2

    An example of code that changes the background color of a text field in UISeachController:

    class ViewController: UIViewController { let searchController = UISearchController(searchResultsController: nil) private lazy var searchTextField: UITextField? = { [unowned self] in var textField: UITextField? searchController.searchBar.subviews.forEach({ view in view.subviews.forEach({ view in if let view = view as? UITextField { textField = view } }) }) return textField }() override func viewDidLoad() { super.viewDidLoad() searchController.obscuresBackgroundDuringPresentation = false searchController.searchBar.placeholder = "Search Candies" navigationItem.searchController = searchController definesPresentationContext = true if let bg = self.searchTextField?.subviews.first { bg.backgroundColor = .green bg.layer.cornerRadius = 10 bg.clipsToBounds = true } } } 

    Result:

    enter image description here

      Try something similar to customize UISearchBar :

       func configureSearchBar() { searchBar.layer.borderWidth = 0 searchBar.backgroundColor = UIColor.white searchBar.backgroundImage = UIImage() searchBar.barTintColor = UIColor.white if let textField = self.searchBar.value(forKey: "searchField") as? UITextField { textField.font = UIFont.systemFont(ofSize: 23, weight: UIFont.Weight.light) textField.textColor = UIColor(withHex: 0x000000) textField.borderStyle = .none textField.leftViewMode = .never textField.textAlignment = .left if let clearButton = textField.value(forKey: "clearButton") as? UIButton { clearButton.setImage(UIImage(named: "searchClearIco")!, for: .normal) clearButton.setImage(UIImage(named: "searchClearIcoTap")!, for: .highlighted) } } }