问题
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
and the second class:
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()
}
}
回答1:
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.
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.
回答2:
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
Make sure that cells in the SearchResultsController
are also of "Subtitle" style.
回答3:
Besides doing what the other users suggested, I also did the following, and it worked:
searchController.definesPresentationContext = true
来源:https://stackoverflow.com/questions/30491613/uisearchcontroller-keyboard-not-showing