How to Change Uitextfield Color in Searchcontroller

how to change uitextfield color in searchcontroller?

This will help you to achieve, what you want. Just apply your color combination in this code and see.

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 = // Set text color
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

}

Result:

Sample Image

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

Here is a an example on how to set the textField background.

class ViewController: UIViewController {

let searchController = UISearchController(searchResultsController: nil)

private lazy var searchTextField: UITextField? = { [unowned self] in
var textField: UITextField?
self.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

Sample Image

How to change UISearchController SearchBar Colour in iOS 11

I have put the code in the main queue and its work.

DispatchQueue.main.async {
self.searchBarController.searchBar.placeholder = "Search"
self.searchBarController.searchBar.setTextColor(color: .white)
self.searchBarController.searchBar.setPlaceholderTextColor(color: .white)
self.searchBarController.searchBar.setSearchImageColor(color: .white)
self.searchBarController.searchBar.setTextFieldColor(color: UIColor.clear) //light green
self.searchBarController.searchBar.delegate = self
}

Strange :)

Changing the color of the icons in a UItextField inside a UISearchBar

Here is the solution:

// Text field in search bar.
let textField = searchController.searchBar.value(forKey: "searchField") as! UITextField

let glassIconView = textField.leftView as! UIImageView
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = UIColor.white

let clearButton = textField.valueForKey("clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = UIColor.white

How can I change the UISearchBar search text color?

You have to access the UITextField inside the UISearchBar. You can do that by using valueForKey("searchField")

var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField

textFieldInsideSearchBar?.textColor = yourcolor

Swift 3 update

let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField

textFieldInsideSearchBar?.textColor = yourcolor


Related Topics



Leave a reply



Submit