How to Customise UIsearchcontroller

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:

Sample Image

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:

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 :)

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



Leave a reply



Submit