如何以编程方式在UINavigationController的左栏按钮中显示UISearchController并在搜索后隐藏它?

时间:2017-08-16 06:35:02

标签: swift uinavigationbar uisearchcontroller

我想在UINavigationController中显示一个UISearchController,当点击UINavigationController的右栏按钮时再次点击该按钮时隐藏它。

Initially the UINavigationBar will be like this

后来,

The UINavigationBar should look like this, when search is clicked

并且当点击关闭图标时,UINavigationBar应该看起来像在image1中。

我用来显示searchcontroller的代码是

 func setUpSearchBar(){
        self.searchController = UISearchController(searchResultsController:  nil)

        self.searchController.searchBar.showsCancelButton = true
        var cancelButton: UIButton

        let topView: UIView = self.searchController.searchBar.subviews[0] as UIView
        for subView in topView.subviews {
            if let pvtClass = NSClassFromString("UINavigationButton") {
                if subView.isKind(of: pvtClass) {
                    cancelButton = subView as! UIButton

                    cancelButton.setTitle("", for: .normal)
                    cancelButton.tintColor = UIColor.blue
                    cancelButton.setImage(UIImage(named:"close"), for: .normal)
                    cancelButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
                }
            }

        }

        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name:"Futura-Medium", size:20)!,NSForegroundColorAttributeName:UIColor(red: 234/255, green: 234/255, blue: 234/255, alpha: 1.0)]

        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true
        self.searchController.searchBar.tintColor = UIColor(red: 56/255, green: 192/255, blue: 201/255, alpha: 1.0)
        self.searchController.searchBar.backgroundColor = UIColor.clear
        self.searchController.searchBar.barStyle = .black
       // self.navigationItem.titleView = searchController.searchBar
        let leftNavBarButton = UIBarButtonItem(customView: self.searchController.searchBar)
        self.navigationItem.leftBarButtonItem = leftNavBarButton
        self.searchController.searchBar.becomeFirstResponder()
    }
   func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        self.searchController.searchBar.resignFirstResponder()
        self.navigationItem.leftBarButtonItem?.isEnabled = false
        self.navigationItem.leftBarButtonItem?.tintColor = .clear

    }
    func pressButton(button: UIButton) {
        searchEvent.isEnabled = true

    }
    func updateSearchResults(for searchController: UISearchController) {
        print(searchController.searchBar.text)
    }

但是这段代码显示了导航栏,但是当点击取消按钮时,pressButton事件没有被触发?为什么会这样?

1 个答案:

答案 0 :(得分:1)

您只需将hidesNavigationBarDuringPresentation属性设置为true即可隐藏导航栏并在该区域显示搜索栏。