我的navigationBar
中有3个导航条项目,如下所示:
func setupNavBarButtons() {
let searchImage = UIImage(named: "search_icon")?.imageWithRenderingMode(.AlwaysOriginal)
let searchBarButtonItem = UIBarButtonItem(image: searchImage, style: .Plain, target: self, action: #selector(handleSearch))
let mapBarButtonItem = UIBarButtonItem(title: "Map", style: .Plain, target: self, action: #selector(displayMap))
navigationItem.rightBarButtonItems = [mapBarButtonItem , searchBarButtonItem]
let filterBarButtonItem = UIBarButtonItem(title: "Filter", style: .Plain , target: self, action: #selector(displayFilter))
navigationItem.leftBarButtonItem = filterBarButtonItem
}
当我点按搜索图标时,它会调用以下函数:
func handleSearch() {
self.navigationItem.titleView = searchController.searchBar
}
我想在用户搜索时隐藏所有navigationBarItems
,然后在用户完成搜索后返回导航栏项目
答案 0 :(得分:1)
您可以在handleSearch
方法中尝试删除左右栏按钮项,然后在searchBarCancelButtonClicked
方法UISearchBarDelegate
上再次设置条形项目。< / p>
func handleSearch() {
searchController.searchBar.hidden = false
self.navigationItem.titleView = searchController.searchBar
searchController.searchBar.becomeFirstResponder()
navigationItem.rightBarButtonItems = nil
navigationItem.leftBarButtonItems = nil
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.setupNavBarButtons()
searchController.searchBar.hidden = true
}