UISearchController - 键盘未显示 [英] UISearchController - Keyboard not showing
问题描述
我正在使用 UISearchController 并且除了两件事外,搜索功能有效.也许它们是相关的:a) 键盘不显示.所以我不能按搜索"按钮b) 在我的主 TableView 中,我访问了样式为Subtitle"的原型单元格.搜索进行时,显示的单元格为基本"样式.在这两种情况下(TableViewController 和 SearchViewController),我使用相同的单元格标识符.
I am using the UISearchController and the search function works except for two things. Perhaps they are related: a) The keyboard does not show. So I cannot press the "Search" button b) In my main TableView I access the prototype cell that has the style "Subtitle". When the search is going on, the cell that shows is "Basic" style. In both cases (TableViewController and SearchViewController) I use the same cell identifier.
有什么想法吗?部分代码如下:
Any ideas? Here some of the code:
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 以及你的子类并且你正在设置
I had the same issue and basically if your parent class has a UISearchController as well as your child class and you're setting the
self.definesPresentationController = YES
在您的父类中,您子类的搜索控制器不会显示键盘.我花了一段时间才弄清楚,但这就是正在发生的事情.
in your parent class, your child class' search controller won't have the keyboard showing. Took me a while to figure out but this is what is happening.
解决方法是在父类即将推送子类时设置self.definesPresentationController = NO
,并设置self.definesPresentationController = YES
父类的 viewWillAppear
...
The way to fix it is to set the self.definesPresentationController = NO
when your parent class is about to push the child class and set self.definesPresentationController = YES
in the viewWillAppear
of your parent class...
希望这也能帮助到那里的人.
Hope this helps someone out there too.
这篇关于UISearchController - 键盘未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!