Ios 如何以编程方式将搜索栏添加到UITableView标题?
如何以编程方式将搜索栏添加到uitableview标题中?具体地说,我有一个条形按钮项,当按下它时,我想在搜索栏中将其设置为表格视图标题的动画,当按下“取消”按钮时,它将设置为视图外的动画,标题将缩放回正常状态。这可能吗?谢谢。它基于Swift 2,但与Swift 3没有太大区别 将搜索委托添加到视图控制器:Ios 如何以编程方式将搜索栏添加到UITableView标题?,ios,swift,uitableview,uisearchbar,uiviewanimation,Ios,Swift,Uitableview,Uisearchbar,Uiviewanimation,如何以编程方式将搜索栏添加到uitableview标题中?具体地说,我有一个条形按钮项,当按下它时,我想在搜索栏中将其设置为表格视图标题的动画,当按下“取消”按钮时,它将设置为视图外的动画,标题将缩放回正常状态。这可能吗?谢谢。它基于Swift 2,但与Swift 3没有太大区别 将搜索委托添加到视图控制器: UISearchResultsUpdating searchController.searchResultsUpdater = self searchController.dimsBac
UISearchResultsUpdating
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
进行搜索:
let searchController = UISearchController(searchResultsController: nil)
为searchController设置以下默认值:
UISearchResultsUpdating
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
将搜索添加到表:
table.tableHeaderView = searchController.searchBar
此委托让您知道开始搜索:
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
实现您的搜索功能:
func filterContentForSearchText(searchText: String, scope: String = "All") {
// do some stuff
}
以编程方式设置搜索栏 首先将UISearchBarDelegate添加到您的类中
let searchBar = UISearchBar()
searchBar.frame = CGRect(x: 0, y: 0, width: 200, height: 70)
searchBar.delegate = self
searchBar.showsCancelButton = true
searchBar.searchBarStyle = UISearchBarStyle.default
searchBar.placeholder = " Search Here....."
searchBar.sizeToFit()
并将搜索栏添加到UITableView标题
tableView.tableHeaderView = searchBar