[英]iOS - How to add UISearchController.searchBar to a controller without a UITableView?
I've looked for tutorials of UISearchController
and all of them are implemented this way: 我一直在寻找
UISearchController
教程,并且所有这些都是以这种方式实现的:
├── MainTableViewController(UITableViewController*)
├── SearchController(UISearchController*)
└── ResultTableViewController(UITableViewController*)
In this way, the SearchController.searchBar
can be displayed at the top of the view simply by 这样,可以简单地通过以下方式将
SearchController.searchBar
显示在视图顶部:
MainTableViewController.tableView.tableHeaderView = SearchController.searchBar;
How can I add the searchBar
to the top of a controller which is not a UITableViewController
? 如何将
searchBar
添加到不是UITableViewController
的控制器的顶部?
In your storyboard select your viewController and open Utilities(top right icon) and search for UISearchBar
. 在情节提要中,选择viewController并打开Utilities(右上角的图标),然后搜索
UISearchBar
。 Now drag the UISearchBar
into your viewController and set frame/constraints. 现在,将
UISearchBar
拖动到您的viewController中并设置框架/约束。 And make a coonection for added searchbar to your viewController class. 并为添加到您的viewController类的搜索栏。
In side your viewController class set delegate for search bar 在您的viewController类中设置搜索栏的委托
override func viewDidLoad()
{
super.viewDidLoad()
searchBar.delegate = self
}
Now implement searchBar:textDidChange
method 现在实现
searchBar:textDidChange
方法
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
{
}
It works just fine without a table view. 没有表视图就可以正常工作。 The UISearchDisplayController instance needs a searchResultsDatasource and delegate, which are really UITableView datasource and delegate.
UISearchDisplayController实例需要searchResultsDatasource和委托,它们实际上是UITableView数据源和委托。
Since these implementations are present in UITableViews, they are often reused as the implementations for the search controller, but that needn't be the case. 由于这些实现都存在于UITableViews中,因此它们经常被重用作为搜索控制器的实现,但事实并非如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.