swiftDevStart
swiftDevStart

Reputation: 101

How to Programmatically show UISearchController in UINavigationController's leftbarbuttonitem and hide it after search?

I want to show a UISearchController within UINavigationController, when clicked on UINavigationController's rightbarbuttonitem and hide it when clicked on the button again.

Initially the UINavigationBar will be like this

Later,

The UINavigationBar should look like this, when search is clicked

and further when clicked on close icon, the UINavigationBar should look like in image1.

The code I'm using to show searchcontroller is,

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

But this code shows navigation bar,but when clicked on cancel button, the pressButton event isn't getting triggered?why is that?

Upvotes: 0

Views: 1735

Answers (1)

Javal Nanda
Javal Nanda

Reputation: 1838

You can simply set hidesNavigationBarDuringPresentation property to true which manages to hide Navigation Bar and show a search bar in that area.

Upvotes: 1

Related Questions