我终于解决了这个问题,用一个简单的(r) UISearchBar 替换了 UISearchController。
也许不是你想听到的答案,但 UISsearchController 在 iOS12 上已经是一团糟,iOS13 上的相同代码可以工作,但会产生可怕的 UI 伪影。就像搜索栏与标题消失或重叠,搜索栏和表格的第一个元素之间的空白,或者将第一个列表项隐藏在范围按钮下,...... iOS12 和 13 之间的所有不同问题,但从来都不是很好。
所以总的来说,我花了 6 个小时尝试修复搜索控制器,但失败了,然后花了 30 分钟迁移到搜索栏。
我只是在 Xcode10.3 中使用 Interface Builder 添加了 UISearchBar。
对于重构,大多数情况下我必须简单地将 searchController.searchBar.xx 替换为 searchBar.xx 。主要工作是重新实现 UISeachBarDelegates。只是在用户搜索时仅显示范围按钮和取消按钮,然后将其删除。下面的代码很好地概述了我所做的事情:
class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var fetchedItemsController: NSFetchedResultsController<Item>! = NSFetchedResultsController()
@IBOutlet weak var searchBar: UISearchBar! //hooked up to IB
//GONE IS: let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
initializeFetchedResultsControllerForItems()
//Enable search controller
searchBar.scopeButtonTitles = [NSLocalizedString("Name", comment: ""),
NSLocalizedString("Birthdate", comment: ""),
NSLocalizedString("Employer", comment: "") ]
searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchBar.delegate = self
searchBar.showsScopeBar = false
searchBar.showsCancelButton = false
tableView.contentInsetAdjustmentBehavior = .automatic
self.tableView.tableHeaderView = searchBar //add the searchbar as tableheader view
self.initializeFetchedResultsControllerForItems()
}
// MARK: - Data loading from CoreData
private func initializeFetchedResultsControllerForItems(searchText: String = "", scopeIndex: Int = 0) {
//print("FETCH RESULTS WITH FILTER: \(searchText) en SCOPE: \(scopeIndex)")
//Do whatever searches you need to do to update the FetchedResultsController
//..
self.tableView.reloadData()
}
}
extension MasterViewController: UISearchBarDelegate { //the delegates for the searchbar
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.showsScopeBar = true //show the scopebar when users adds text to searchbar
searchBar.showsCancelButton = true //also show the cancel button
searchBar.sizeToFit()
self.tableView.reloadData() //since the scopebar is there, the table needs to move a bit down
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
initializeFetchedResultsControllerForItems(searchText: searchBar.text!, scopeIndex: searchBar.selectedScopeButtonIndex)
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
switch (selectedScope) {
case 0: searchBar.placeholder = NSLocalizedString("Seach on name", comment: "")
case 1: searchBar.placeholder = NSLocalizedString("Search on birthdate", comment: "")
case 2: searchBar.placeholder = NSLocalizedString("Search on employer", comment: "")
default: searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchBar.showsScopeBar = true
searchBar.sizeToFit()
}
initializeFetchedResultsControllerForItems(searchText: searchBar.text!, scopeIndex: selectedScope)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchBar.showsScopeBar = false
searchBar.showsCancelButton = false
searchBar.endEditing(true)
searchBar.text = ""
searchBar.sizeToFit()
initializeFetchedResultsControllerForItems(searchText: searchBar.text!, scopeIndex: searchBar.selectedScopeButtonIndex)
}
}