How to customize the searchBar in a UISearchController?
set attributedPlaceholder
for textfield of search bar
@IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.backgroundColor = UIColor.red
textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
if let leftView = textfield.leftView as? UIImageView {
leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
leftView.tintColor = UIColor.white
}
}
Here is result:
Update:
I think, this may help you: how to change uitextfield color in searchcontroller?
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 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 :)
How to resizing issue of UISearchController.searchBar when focused?
As surToTheW suggested in his comment, I have created a custom UIViewController with UISearchController in it. I added it as child view controller in to my main UIViewController and the search bar didn't exceed the custom UIViewController's view width.
Related Topics
How to Decorate Siesta Request with an Asynchronous Task
How to Show an Alert in Swift UIalertview Not Working
Can't Update Label from Other Class in Swift
How to Download Datas from Multiple Url in Concurrency Mode
How to Detect Encoding in Data Based on a String
Qlpreviewcontroller Showing File Then Going Blank in Swiftui
Set Maximum Characters (To One) in a Nstextfield in Swift
Turn Off Splash Screen When Using Flutterviewcontroller Within Existing Native App
Swift: Change Speed of a Moveto Skaction
Generic Return Type Based on Class
Spritekit Skscene Not Resizing Correctly to Fit iPhone 12
How to Add a Storage Reference in Swift for Firestore
Filter by Day from Nsdate in Realm, Swift
Redirect Process Stdout to Apple System Log Facility in Swift
Why Can't I Get The Index of a Filtered Realm List
Uibutton State Changing While Scrolling The Tableview with Multiple Sections - Swift