我正在使用 UISearchController 并且搜索功能可以正常工作,但有两点除外。也许它们是相关的: a) 键盘不显示。所以我不能按“搜索”按钮 b) 在我的主 TableView 中,我访问了具有“副标题”样式的原型(prototype)单元格。进行搜索时,显示的单元格为“基本”样式。在这两种情况下(TableViewController 和 SearchViewController)我都使用相同的单元格标识符。
有什么想法吗?这里有一些代码:
class OverviewTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let resultsController = SearchResultsController()
resultsController.birds = birds
searchController = UISearchController(searchResultsController: resultsController)
//searchController.dimsBackgroundDuringPresentation = true
let searchBar = searchController.searchBar
searchBar.scopeButtonTitles = ["One", "Two"]
searchBar.placeholder = "Search"
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController
第二类:
class SearchResultsController: UITableViewController, UISearchResultsUpdating {
var items: [Item] = []
var filtered: [Item] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
// MARK: - UISearchResultsUpdating Conformance
func updateSearchResultsForSearchController(searchController: UISearchController) {
//searchController.hidesNavigationBarDuringPresentation = true
let searchString = searchController.searchBar.text
let buttonIndex = searchController.searchBar.selectedScopeButtonIndex
filtered.removeAll(keepCapacity: true)
if !searchString.isEmpty {
for item in items {
...
}
}
self.tableView.reloadData()
}
}
最佳答案
我有同样的问题,基本上如果你的父类有一个 UISearchController 以及你的子类并且你正在设置
self.definesPresentationController = YES
在您的父类中,子类的搜索 Controller 不会显示键盘。我花了一段时间才弄明白,但这就是正在发生的事情。
解决方法是在你的父类要push子类的时候设置self.definesPresentationController = NO
,在你父类的 viewWillAppear
...
希望这也能帮助其他人。
关于ios - UISearchController - 键盘不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30491613/