UISearchController 中的导航 iOS11
Navigation in UISearchController iOS11
我正在尝试实现与 WhatsApp 中相同的行为。我进入聊天下拉菜单,输入我的搜索,然后会显示一个新的 TableView,其中包含搜索结果。
到目前为止没有问题。它看起来像这样:
let searchController = UISearchController(searchResultsController: UITableViewController())
searchController.searchResultsUpdater = self
definesPresentationContext = true
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
}
现在是毛茸茸的部分:当我单击结果时,我希望它像在 UINavigationController 中一样显示。就像在 WhatsApp 中一样。当我点击返回时,我想看到我的搜索结果。
我尝试了各种方法。将 searchResultsController 嵌入到 UINavigationController 中。玩转 definesPresentationContext,等等。也许你们中有人为此找到了解决方案。
出于性能原因,我不希望搜索结果就地显示。在 searchResultsController 中必须有一个包含所有结果的解决方案。
我非常感谢任何解决此问题的建议和解决方案。
我终于找到答案了!!!
解决方案是:为您的 searchResultsController 创建一个子类,当您实例化控制器时将 UINavigationController 的引用传递给它。在选择您称之为的单元格时:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// yourController is the UIViewController to show your stuff
if let yourController = storyboard?.instantiateViewController(withIdentifier: "yourController"){
// navController is the reference to the navController of the controller where you performed the search
navController?.pushViewController(yourController, animated: true)
}
}
我正在尝试实现与 WhatsApp 中相同的行为。我进入聊天下拉菜单,输入我的搜索,然后会显示一个新的 TableView,其中包含搜索结果。
到目前为止没有问题。它看起来像这样:
let searchController = UISearchController(searchResultsController: UITableViewController())
searchController.searchResultsUpdater = self
definesPresentationContext = true
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
}
现在是毛茸茸的部分:当我单击结果时,我希望它像在 UINavigationController 中一样显示。就像在 WhatsApp 中一样。当我点击返回时,我想看到我的搜索结果。
我尝试了各种方法。将 searchResultsController 嵌入到 UINavigationController 中。玩转 definesPresentationContext,等等。也许你们中有人为此找到了解决方案。
出于性能原因,我不希望搜索结果就地显示。在 searchResultsController 中必须有一个包含所有结果的解决方案。
我非常感谢任何解决此问题的建议和解决方案。
我终于找到答案了!!! 解决方案是:为您的 searchResultsController 创建一个子类,当您实例化控制器时将 UINavigationController 的引用传递给它。在选择您称之为的单元格时:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// yourController is the UIViewController to show your stuff
if let yourController = storyboard?.instantiateViewController(withIdentifier: "yourController"){
// navController is the reference to the navController of the controller where you performed the search
navController?.pushViewController(yourController, animated: true)
}
}