Ios DefinePresentationContext应设置为YES,但与UISearchController结合使用时会中断导航
对于在常规的Ios DefinePresentationContext应设置为YES,但与UISearchController结合使用时会中断导航,ios,swift,Ios,Swift,对于在常规的UIViewController中使用UISearchController的设置(也有带有某些项目的表视图),我得到以下警告包含显示的搜索控制器的导航控制器的topViewController必须将DefinePresentationContext设置为YES 但是,在ViewController上设置DefinePresentationContext=true会破坏我在搜索处于活动状态时在NavigationController上推送新ViewController的能力,这首先会破
UIViewController
中使用UISearchController
的设置(也有带有某些项目的表视图),我得到以下警告包含显示的搜索控制器的导航控制器的topViewController必须将DefinePresentationContext设置为YES
但是,在ViewController上设置DefinePresentationContext=true
会破坏我在搜索处于活动状态时在NavigationController上推送新ViewController的能力,这首先会破坏搜索的目的(我想搜索,然后如果用户点击结果,则将其推送到导航堆栈上)
在尝试推送新的ViewController之前,我已经设置了searchController.isActive=false
在推送另一个视图之前,我还需要做什么才能关闭UISearchController
// The ViewController is presented inside a UINavigationController
class ViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
// If not set to true, triggers the following error:
// "The topViewController of the navigation controller containing
// the presented search controller must have definesPresentationContext set to YES"
definesPresentationContext = true
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
navigationItem.searchController.isActive = false
// If definesPresentationContext is true, triggers the following
// error when the search bar is/was focused at the time of selection:
// "pushViewController:animated: called on UINavigationController while an existing transition
// or presentation is occurring; the navigation stack will not be updated."
navigationController?.pushViewController(UIViewController(), animated: true)
}
}
事实证明,
navigationItem.searchController.isActive=false
将以动画方式关闭搜索栏。因此,此时仍在进行转换,导致“第二次”导航失败
在UIView.performWithoutAnimation
中调用块也不起作用
因此,解决方案是使用UISearchController.Disclose(动画:完成)
,即
searchController.dismiss(animated: false) {
navigationController?.pushViewController(UIViewController(), animated: true)
}