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:
Connect UISearchController to UISearchBar without embedding the search bar in the tableview or in the navigation bar
You did all the hard work and the thing that you miss is fairly trivial to what you did.
Basically you should add the UISearchBar
resultSearchController!.searchBar
as the subview to the view using the code. I don't think it would be possible with Storyboard, that could be what confused you.
Related Topics
iOS Facebook Login "Given Url Is Not Allowed by the Application Configuration"
Nspredicate to Match "Any Entry in an Nsdatabase with Value That Contains a String"
How to Access the Accelerometer from the Apple Watch
How to Register Undomanager in Swift
Swift- Custom Uitableviewcell Delegate to Uiviewcontroller Only One Protocol Works
Hide Status Bar and Increase the Height of Uinavigationbar
Create a Tabbar Controller with a Master-Detail Template
Is Float, Double, Int an Anyobject
How to Solve This Exc_Bad_Access(Code=Exc_I386_Gpflt )In Swift Programming
Avplayer with Playback Controls of Avplayerviewcontroller
How to Properly Add Child View Controller in iOS 8 with Swift
Which Phassetcollection to Use for Saving an Image
Swift - Add Gesture Recognizer to Object in Table Cell
Get the Expiration Date of a Provisioning Profile at Run-Time