Ahemadabbas Vagh
Ahemadabbas Vagh

Reputation: 494

UISearchBarSearchField BackgroundView color

enter image description hereI am trying to change the background color of the search bar text field and i have searched and tried lots of solutions but those are not working.

So, please anyone can tell me the exact solution for that. How can we change the background color of the search bar text field?

/// used to prepare searchController inside navigation.
private func prepareNavigationSearchControllerSetup() {
    self.title = AppConstant.kContacts
    let search = UISearchController(searchResultsController: nil)
    search.searchResultsUpdater = self
    search.searchBar.cornerRadius = 10.0
    search.searchBar.textField?.backgroundColor = .red
    search.searchBar.textField?.tintColor = .yellow
    self.navigationItem.searchController = search
    self.navigationItem.hidesSearchBarWhenScrolling = false
}

extension UISearchBar {

    var textField: UITextField? {
        let subViews = subviews.flatMap { $0.subviews }
        return (subViews.filter { $0 is UITextField }).first as? UITextField
    }
}

Upvotes: 1

Views: 2510

Answers (5)

Sigit
Sigit

Reputation: 91

maybe in the future someone still needs it, I tried this code and it works!

if let searchTextField = searchBar.value(forKey: "searchField") as? UITextField {
    searchTextField.backgroundColor = UIColor.green // Change this to the desired color
}

Upvotes: 0

michael23
michael23

Reputation: 3934

For iOS 17, the only thing that worked for me was SuperDuperTango's answer here and I did the following to remove that overlay.

searchBar.setSearchFieldBackgroundImage(UIImage(), for: .normal)

and then re-apply a corner radius

searchBar.searchTextField.layer.cornerRadius = 8.0

Upvotes: 0

pipacs
pipacs

Reputation: 1109

Since iOS 13:

        if #available(iOS 13.0, *) {
            searchController.searchBar.searchTextField.backgroundColor = .red
            searchController.searchBar.searchTextField.tintColor = .yellow
        }

Upvotes: 0

Ahemadabbas Vagh
Ahemadabbas Vagh

Reputation: 494

After a lot more search I found the correct answer that is working for me.

if #available(iOS 11.0, *) {
        if let textfield = search.searchBar.value(forKey: "searchField") as? UITextField {
            textfield.textColor = UIColor.blue

            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.white
                backgroundview.layer.cornerRadius = 10;
                backgroundview.clipsToBounds = true;
            }
        }

  }

Upvotes: 3

dahiya_boy
dahiya_boy

Reputation: 9503

You can try this

UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue

Output

enter image description here

Edit : Full Code

    var searchController = UISearchController()
    let resultsTableController = Storyboard.Home.instantiateViewController(withIdentifier: "GlobalTableVC") as! GlobalTableVC
    resultsTableController.tableView.delegate = self
    resultsTableController.tableView.dataSource = self

    resultsTableController.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SearchCell")

    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchBar.placeholder = "Search"
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.sizeToFit()
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.keyboardType = UIKeyboardType.alphabet
    searchController.searchBar.tintColor = UIColor.white
    searchController.searchBar.barTintColor = UIColor(hexString: "EB033B")

    UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
    UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue


    searchController.searchBar.delegate = self
    searchController.delegate = self
    searchController.searchResultsUpdater = self

    present(searchController, animated: true, completion: nil)

Upvotes: 1

Related Questions