我有一个 UISearchController
,在 NavigationBarTitleView
中有一个 UISearchBar
。当您开始在 SearchBar
中键入内容时,会出现一个 ResultsTableView
。键入时 SearchBar
旁边会出现一个取消按钮,它会正确关闭 TableView
。到目前为止,一切都很好。
场景的主 viewController 是一个 TableViewController
,它连接到 TabBarController
中的 NavigationController
。
现在,一切正常,除了当显示 ResultsTableView
并且您离开该屏幕(通过 tabBar
)时,当您返回时屏幕变黑
我想我需要在导航离开时关闭 resultsTableView
以防止这种情况(就像我在触发结果的 segue 时所做的那样)但我无法让它工作。
这是一些代码:
class PCFeedTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating, UISearchControllerDelegate {
let searchController = UISearchController(searchResultsController: PCSearchTableViewController())
func updateSearchResultsForSearchController(searchController: UISearchController) {
let resultsTable = searchController.searchResultsController as! PCSearchTableViewController
resultsTable.tableView.frame.origin = CGPoint(x: 0, y: 64)
resultsTable.tableView.frame.size.height = self.tableView.frame.height - 113
resultsTable.tableView.tableHeaderView = nil
resultsTable.tableView.contentInset = UIEdgeInsetsZero
resultsTable.tableView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
resultsTable.parentController = self
resultsTable.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "searchResultCell")
}
...
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.scrollsToTop = true
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
self.searchController.searchBar.placeholder = "Search for Movies or TV Shows"
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.searchController.searchBar.barStyle = UIBarStyle.Black
UILabel.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.lightTextColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
self.navigationItem.titleView = searchController.searchBar
}
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationViewController = segue.destinationViewController as! PCMainDetailsTableViewController
self.searchController.searchResultsController?.dismissViewControllerAnimated(true, completion: nil)
self.searchController.searchBar.text = nil
destinationViewController.title = searchResult.title
destinationViewController.movie = searchResult
}
}
最佳答案
尝试将关闭搜索 Controller 的代码放在 viewDidDisappear:
中,如下所示:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.searchController.searchResultsController?.dismissViewControllerAnimated(true, completion: nil)
self.searchController.searchBar.text = nil
}
关于iOS UISearchController TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34199411/