如何以编程方式将搜索栏添加到UITableView标头?

时间:2017-02-13 09:00:13

标签: ios swift uitableview uisearchbar uiviewanimation

如何以编程方式将搜索栏添加到uitableview标题中?具体来说,我有一个条形按钮项,当它被按下时,我想在搜索栏中设置动画到表视图标题中,当按下取消按钮时,它将动画回到视图之外,标题缩放回正常。这可能吗?感谢。

2 个答案:

答案 0 :(得分:3)

它基于 Swift 2 ,但它与swift 3不太相似

将搜索委托添加到视图控制器:

UISearchResultsUpdating

进行搜索:

let searchController = UISearchController(searchResultsController: nil)

为searchController设置此默认值:

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
}

答案 1 :(得分:1)

以编程方式设置搜索栏

首先将 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