在搜索过程中隐藏导航栏项目

时间:2016-08-04 15:00:50

标签: ios swift

我的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,然后在用户完成搜索后返回导航栏项目

1 个答案:

答案 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
}