gpt4 book ai didi

iOS 13 如何在导航栏中隐藏 UISearchController searchBar

转载 作者:行者123 更新时间:2023-12-04 16:08:26 48 4
gpt4 key购买 nike

我有带搜索栏的导航栏(UISearchController)
我有左栏按钮图标,单击该图标时,通过将其分配给 navigationItem 来显示此搜索 Controller ,如下所示:

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()
}

它可以工作,但是如果在取消按钮触摸时我尝试隐藏搜索栏,那么我将关闭 View Controller 并出现黑屏(没有 View Controller )
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 使用标题 + 搜索 Controller 将它动画到扩展导航栏,然后点击搜索图标正确隐藏搜索 Controller 。所以问题是我认为在为扩展导航栏设置动画时删除搜索 Controller

最佳答案

super 蹩脚的 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()
}
}

关于iOS 13 如何在导航栏中隐藏 UISearchController searchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58078909/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com