[英]Floating/fixed search bar above TableView - Swift 3
我有一個問題,許多其他用戶都問在計算器上(例如: 1 , 2 , 3 )。
滾動桌面視圖時,我想要一個固定的搜索欄 。 我最初有一個帶UISearchBar
的UITableViewController
。
大多數解決方案建議我應該有一個帶有UISearchBar
的UIViewController
和一個UITableView
。 我做到了這一點。 下面是我的界面構建器的屏幕截圖。
但是,此解決方案無法修復它:(搜索欄仍然不會浮動,滾動時它會隱藏在導航欄后面。
我也試過這個解決方案,但我寧願在導航欄中沒有搜索欄。
這是我的所有TableView或搜索相關代碼(我刪除了所有不相關的代碼):
class NumberSelectorViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var tableView: UITableView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchController.searchResultsUpdater = self as UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// NOTE: Removing thie line removes the search bar entirely.
tableView.tableHeaderView = searchController.searchBar
addCancelButton()
}
// MARK: - Table view data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching() == true {
return filteredNumberList.count
}
return NumberList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "numberCell")! as UITableViewCell
if isSearching() == true {
cell.textLabel?.text = filteredNumberList[indexPath.row].NumberName
} else {
cell.textLabel?.text = NumberList[indexPath.row].NumberName
}
return cell
}
// MARK: - Table view delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if isSearching() == true {
selectedNumber = filteredNumberList[indexPath.row]
} else {
selectedNumber = NumberList[indexPath.row]
}
performSegue(withIdentifier: "someSegue", sender: self)
}
// MARK: Searching
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredNumberList = NumberList.filter({ (Number) -> Bool in
return (Number.NumberName?.lowercased().contains(searchText.lowercased()))!
})
tableView.reloadData()
}
// MARK: Search Bar
func isSearching() -> Bool {
return (searchController.isActive && searchController.searchBar.text != "")
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredNumbersList.removeAll()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.tableView.reloadData()
}
extension NumberSelectorViewController: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
}
我實際上發現了答案。 問題只是我有這條線:
tableView.tableHeaderView = searchController.searchBar
我刪除了這個,然后搜索欄消失了,但它只是隱藏在導航欄后面! 所以我玩了界面構建器上的約束,它現在正常工作:)
您似乎已將搜索欄正確添加到視圖中,它只是隱藏在導航欄下方。 試試viewDidLoad
self.edgesForExtendedLayout = UIRectEdgeNone;
並擺脫
tableView.tableHeaderView = searchController.searchBar
您不需要在tableView或其標題中添加searchBar。
在viewDidLoad方法中,您可以提供Edge Inset,以便tableView中的內容不會被searchBar重疊
override func viewDidLoad() {
super.viewDidLoad()
let edgeInsets = UIEdgeInsetsMake(52, 0, 0, 0)
self.tableView.contentInset = edgeInsets
self.tableView.register(UINib(nibName: "AbcCell", bundle: nil), forCellReuseIdentifier: "AbcCell")
}
以下是我將其放置在我的一個項目中並且它不會漂浮的圖像。
我是另一種觀點,所以你可以把它放在任何地方
anyView.addSubview(searchController.searchBar)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.