Use background image on UISearchController iOS 11
Thanks to Krunal's answer I was able to find a decent solution after searching for quite some time.
This is how I came about implementing the background-image for iOS 11:
// Implement Search Controller
if #available(iOS 11.0, *) {
if let navigationBar = self.navigationController?.navigationBar {
navigationBar.barTintColor = UIColor(patternImage: gradientImage!)
}
if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundview = textField.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}
Change UISearchBar background image in iOS11
You may be looking for this:
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor(patternImage: UIImage(named: "pattern")!)
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
Here is image:
And here is result:
Here is more detailed answer, if you want to change any other UI elements of search bar.
- iOS 11 UISearchBar in UINavigationBar
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 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
Related Topics
Is There a Github Markdown Language Identifier for Swift Code
Zoom to Fit Current Location and Annotation on Map
Swift-Setting a Physics Body Velocity by Angle
How to Add 3D Shapes in Swift Ui
Swift Change the Tableviewcell Border Color According to Data
How to Trigger Xcode's 'Update to Latest Package Versions' from Command Line
Integer Literal Overflows When Stored into 'Int'
How to Replace the Values of Labels in iOS-Charts
Swift Sprite Kit in App Purchase
Convert String to Staticstring
Changing Tab Bar Color (Swift)
Swift Firebase Using an Unspecified Index
How to Perform Face Detection in Swift
Custom Annotation Showing Same Image for All Different Types of Poi'S