How do I put UISearchController Searchbar onto navigation bar with code
如果我将 ViewController 嵌入到导航栏中,
1
2 3 4 5 6 7 8 9 10 11 |
resultSearchController = UISearchController(searchResultsController: locationSearchTable) resultSearchController?.searchResultsUpdater = locationSearchTable let searchBar = resultSearchController!.searchBar let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y:0, width: 375, height: 64)) |
创建一个
将搜索控制器搜索栏添加到 UINavigationItem 作为 titleView.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class SearchViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController: UISearchController! override func viewDidLoad() { func addNavigationbar() { let navigationItem = UINavigationItem(title:"") navBar.setItems([navigationItem], animated: false) func searchControllerWith(searchResultsController: UIViewController?) -> UISearchController { let searchController = UISearchController(searchResultsController: searchResultsController) return searchController |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class UIViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController! override func viewDidLoad() { self.searchController = UISearchController(searchResultsController: nil) self.searchController.searchResultsUpdater = self self.searchController.hidesNavigationBarDuringPresentation = false self.navigationItem.titleView = searchController.searchBar self.definesPresentationContext = true func updateSearchResults(for searchController: UISearchController) { } } |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268677.html