如何以编程方式将搜索栏添加到UITableView标头? [英] How to programmatically add a search bar to a UITableView header?
问题描述
我将如何以编程方式将搜索栏添加到uitableview标头中?具体来说,我有一个栏按钮项目,当按下该按钮时,我想在搜索栏中为表格视图标题添加动画,而当单击取消"按钮时,它将在视图之外进行动画制作,标题重新缩放为正常.这有可能吗?谢谢.
How would i go about programmatically adding a search bar into a uitableview header? Specifically i have a bar button item that when it's pressed i would like to animate in a search bar into the table view header and when the cancel button is pressed it will animate back out of the view and the header scales back to normal. Would this be possible? Thanks.
推荐答案
它基于 Swift 2 ,但是与swift 3差异不大
It's based on Swift 2 but It's not very diff to swift 3
将搜索委托添加到视图控制器:
Add search delegate to view controller:
UISearchResultsUpdating
进行搜索:
let searchController = UISearchController(searchResultsController: nil)
为此searchController设置以下默认值:
Set this default values for searchController:
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
将搜索添加到表中
table.tableHeaderView = searchController.searchBar
此代表让您知道开始搜索:
This delegate let you know start searching:
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
增强您的搜索功能:
func filterContentForSearchText(searchText: String, scope: String = "All") {
// do some stuff
}
这篇关于如何以编程方式将搜索栏添加到UITableView标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!