gpt4 book ai didi

ios - 单击取消按钮时隐藏 searchController searchBar

转载 作者:行者123 更新时间:2023-11-28 08:06:17 26 4
gpt4 key购买 nike

我在带有 TableView 的 viewController 中实现了一个搜索 Controller ,还有一个搜索 Controller ,搜索栏在搜索按钮被录音时显示,搜索按钮是一个 UIBarButtonItem 并且搜索栏显示在导航 Controller 中,但我想要为了在点击取消按钮时消失,我尝试使用 serarchBarDelegate 协议(protocol),但是点击这里的代码没有任何反应

    class NewsTVController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate{
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = true

self.searchController.hidesNavigationBarDuringPresentation = false

loadNews()
}


@IBAction func searchButtonTaped(sender:UIBarButtonItem){
print("tap inside")
self.navigationItem.titleView = searchController.searchBar
//self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
// searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

//}
//self.navigationItem.rightBarButtonItem.
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
hideSearchBar()
}
func hideSearchBar() {
print("hay que escpder")
//navigationItem.setLeftBarButtonItem(searchBarButtonItem, animated: true)
//logoImageView.alpha = 0
UIView.animate(withDuration: 0.3, animations: {
self.navigationItem.titleView = nil// = self.logoImageView
self.navigationItem.title = "Eventos"
//self.logoImageView.alpha = 1
}, completion: { finished in

})
}
}

这是带有搜索按钮和搜索栏的导航 Controller 的图片 enter image description here

我想要的是,当点击取消按钮时,导航栏保持在第一张图片中,没有搜索栏。和一些提示有一种方法可以在出现搜索栏时隐藏后退按钮和搜索按钮。

最佳答案

因为我找不到实现此目标的方法,所以我找到了这个解决方案

@IBAction func searchButtonTaped(sender:UIBarButtonItem){
print("tap inside")
self.navigationItem.titleView = searchController.searchBar
searchController.searchBar.setShowsCancelButton(false, animated: false)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(hideSearchBar(sender:)))
//self.searchIcon.is
//self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
// searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){

//}
//self.navigationItem.rightBarButtonItem.
}
func hideSearchBar(sender:UIBarButtonItem) {
print("hay que escpder")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(searchButtonTaped(sender:)))
//logoImageView.alpha = 0
UIView.animate(withDuration: 0.3, animations: {
self.navigationItem.titleView = nil// = self.logoImageView

self.navigationItem.title = "Noticias"
//self.logoImageView.alpha = 1
}, completion: { finished in

})
}

现在搜索栏从导航 Controller 中消失了,搜索按钮可以正常工作,因为它链接到 Interface Builder 中的 searchButtonTaped

关于ios - 单击取消按钮时隐藏 searchController searchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45014078/

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