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:
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
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
How to Instantiate and Load a View Controller Before Segueing to It Using Swift
Dynamic Uitablecellview Height
Didoutputsamplebuffer Delegate Not Called
Swift iOS Record Video and Audio with Avfoundation
Sirikit, How to Display Response for Start Workout Intent
Operation Went Isfinished=Yes Without Being Started by the Queue It Is In
Scaling Physics Bodies in Xcode Spritekit
How to Reuse Struct on Swift? or Is There Any Other Way
Firebase Checking for Null Value (Swift)
How to Download and View Images from the New Firebase Storage
Uitextfield Starting Cursor Position Is Wrong
Extending a Delegate from a Base Class
Firebase: Provided Bucket Does Not Match the Storage Bucket of the Current Instance in Swift
Modifing One Variable from Another View Controller Swift
Horizontal Paging Uicollectionview with Automatic Item Size in a Vertical Stack View
How to Debug Swift Playgroundbook
Getting Uitableview Error "Unable to Dequeue a Cell with Identifier Cell"