//Variables
fileprivate let searchController = UISearchController(searchResultsController: nil)
//Setup Search Controller
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Search".localized()
self.searchController.searchBar.barStyle = .black
self.searchController.searchBar.delegate = self
self.definesPresentationContext = true
self.navigationItem.searchController = searchController
//MARK: SEARCH BAR DELEGATE
extension ViewController: UISearchBarDelegate
{
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
{
//Show Cancel
searchBar.setShowsCancelButton(true, animated: true)
searchBar.tintColor = .white
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
//Filter function
self.filterFunction(searchText: searchText)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
//Hide Cancel
searchBar.setShowsCancelButton(false, animated: true)
searchBar.resignFirstResponder()
guard let term = searchBar.text , term.trim().isEmpty == false else {
//Notification "White spaces are not permitted"
return
}
//Filter function
self.filterFunction(searchText: term)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
//Hide Cancel
searchBar.setShowsCancelButton(false, animated: true)
searchBar.text = String()
searchBar.resignFirstResponder()
//Filter function
self.filterFunction(searchText: searchBar.text)
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.