Created
August 14, 2018 03:08
-
-
Save geek1706/06f8d14a45d5444a70b937b132a10450 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
final class SearchViewController: UITableViewController { | |
private var searchController: UISearchController! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
searchController = UISearchController(searchResultsController: nil) | |
searchController.searchResultsUpdater = self | |
searchController.searchBar.sizeToFit() | |
if #available(iOS 11.0, *) { | |
// For iOS 11 and later, we place the search bar in the navigation bar. | |
navigationController?.navigationBar.prefersLargeTitles = true | |
navigationItem.searchController = searchController | |
// We want the search bar visible all the time. | |
navigationItem.hidesSearchBarWhenScrolling = false | |
} else { | |
// For iOS 10 and earlier, we place the search bar in the table view's header. | |
tableView.tableHeaderView = searchController.searchBar | |
} | |
searchController.delegate = self | |
searchController.dimsBackgroundDuringPresentation = false // default is YES | |
searchController.searchBar.delegate = self // so we can monitor text changes + others | |
/* | |
Search is now just presenting a view controller. As such, normal view controller | |
presentation semantics apply. Namely that presentation will walk up the view controller | |
hierarchy until it finds the root view controller or one that defines a presentation context. | |
*/ | |
definesPresentationContext = true | |
} | |
} | |
// MARK: - UISearchResultsUpdating | |
extension SearchViewController: UISearchResultsUpdating { | |
func updateSearchResults(for searchController: UISearchController) { | |
guard let text = searchController.searchBar.text else { return } | |
} | |
} | |
// MARK: - UISearchBarDelegate | |
extension SearchViewController: UISearchBarDelegate { | |
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { | |
searchBar.resignFirstResponder() | |
} | |
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { | |
searchBar.resignFirstResponder() | |
} | |
} | |
// MARK: - UISearchControllerDelegate | |
extension SearchViewController: UISearchControllerDelegate { | |
func presentSearchController(_ searchController: UISearchController) { | |
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).") | |
} | |
func willPresentSearchController(_ searchController: UISearchController) { | |
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).") | |
} | |
func didPresentSearchController(_ searchController: UISearchController) { | |
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).") | |
} | |
func willDismissSearchController(_ searchController: UISearchController) { | |
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).") | |
} | |
func didDismissSearchController(_ searchController: UISearchController) { | |
//debugPrint("UISearchControllerDelegate invoked method: \(__FUNCTION__).") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment