[当我将项目更新到iOS 13时,我遇到了异常问题。我有显示和处理某些UISearchController
动作的逻辑(以下代码),所有部分在iOS 11和12中均正常运行。
我的任务是在导航栏上添加搜索按钮,以在按钮操作后显示UISearchController
。
但是在iOS 13中,我遇到了2个问题:
从按钮动作调用的代码
func showSearch() {
let searchResultsController = LUSearchResultsViewController()...
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
searchController.searchResultsUpdater = searchResultsController
navigationItem.searchController = searchController
definesPresentationContext = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
searchController.isActive = true
}
}
[只有一种解决方案可以帮助我显示搜索控制器,而与以前的iOS版本没有任何区别。
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
searchController.isActive = true
}
可在iOS 13上运行的代码,在iOS 11和12上,代码无需asyncAfter即可工作
帮助我隐藏UISearchController
并设置导航栏初始状态的代码在iOS 11和12中,而不是在13中。在SearchBar上点击cancel后,UISearchController
开始调用委托方法。
//MARK: UISearchControllerDelegate
public func willDismissSearchController(_ searchController: UISearchController) {
self.navigationItem.searchController = nil
}
BUT
设置了navigationItem.searchController = nil
之后,didPresentSearchController
调用了两次,不仅取消了UISearchController
,而且还取消了UIViewController
的整个UIWindow
层次结构。或者,如果我在此处添加asyncAfter
,我将获得正常的解雇,并具有扩展的导航栏高度,这是我所不需要的。
SO
我可以为您提供第一部分的帮助。
代替执行异步部分,您可以使用此:
searchController.searchBar.layoutIfNeeded()
searchController.isActive = true
SearchController在那个特定时刻还没有准备好,它需要先布局才能变得可用。
通过对searchBar进行布局,我们强制执行此操作,并且只有在要布局的内容时才会触发。
此问题发生在iOS 13
添加searchController.extendedLayoutIncludesOpaqueBars = true
以及
`
extension SearchViewController:UISearchControllerDelegate{
func willPresentSearchController(_ searchController: UISearchController) {
if #available(iOS 13, *){
self.navigationController?.navigationBar.isTranslucent = true
}
}
func willDismissSearchController(_ searchController: UISearchController) {
if #available(iOS 13, *){
self.navigationController?.navigationBar.isTranslucent = false
}
}
}
`