我有带有搜索栏的导航栏(UISearchController) 我有一个左侧的按钮图标,单击该按钮时,通过将其分配给NavigationItem来显示此搜索控制器,如下所示:
if navigationItem.searchController != nil {
navigationItem.searchController = nil
navigationController?.view.setNeedsLayout()
navigationController?.view.layoutIfNeeded()
} else {
navigationItem.searchController = searchController
navigationController?.view.setNeedsLayout()
navigationController?.view.layoutIfNeeded()
searchController.searchBar.becomeFirstResponder()
}
它可以工作,但是如果在取消按钮上触摸,我试图隐藏搜索栏,那么我就关闭了视图控制器,并且出现黑屏(没有视图控制器)
extension SearchableMenuViewController : UISearchBarDelegate {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
guard #available(iOS 11.0, *) else { return }
guard !isAlwaysVisible else { return }
if #available(iOS 13.0, *) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.navigationItem.searchController = nil
self.navigationController?.view.setNeedsLayout()
self.navigationController?.view.layoutIfNeeded()
}
} else {
navigationItem.searchController = nil
navigationController?.view.setNeedsLayout()
navigationController?.view.layoutIfNeeded()
}
}
我尝试添加延迟原因,原因是没有从NavigationItem中删除此searchcontroller,而是使用Title + Search Controller将其动画化为扩展的导航栏,然后点击Search icon正确隐藏了搜索控制器。所以问题是我认为在动画扩展到导航栏时删除搜索控制器
答案 0 :(得分:2)
超级la脚haxx可以暂时解决您的问题:
func didDismissSearchController(_ searchController: UISearchController) {
if #available(iOS 13, *) {
navigationItem.searchController = nil
self.navigationController?.view.setNeedsLayout()
self.navigationController?.view.layoutSubviews()
let view = UIView()
self.navigationController?.navigationBar.insertSubview(view, at: 1)
view.removeFromSuperview()
}
}