Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tap on the button and open searchBar

I have a tableView with searchBar implemented. In the top of this, I have a button, which should open searchBar with keyboard. Here is how it should look like: User tap here

Open search bar

So, when user tap on this button, it should open the searchBar and user can start typing something. Here is my search methods:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        guard !searchText.isEmpty else { filteredActiveState = activeStates; tableView.reloadData(); return }

        searching = true
        //typeView.isHidden = false

        filteredActiveState = activeStates.filter({ titleName -> Bool in
            return titleName.title!.lowercased().contains(searchText.lowercased())
        })
        tableView.reloadData()
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchBar.isHidden = true
        channelLbl.isHidden = false
        searchButtonOutlet.isHidden = false
        typeView.isHidden = true
        //searchBar.endEditing(true)
        //searchBar.resignFirstResponder()
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.endEditing(true)
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.text = ""
        searchBar.resignFirstResponder()
        self.tableView.reloadData()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        searchBar.endEditing(true)
    }

Here is code for this button:

@IBAction func searchBtnPressed(_ sender: UIButton) {
        searchBar.isHidden = false
        channelLbl.isHidden = true
        searchButtonOutlet.isHidden = true
}

>Solution :

What if you did searchBar.becomeFirstResponder() inside your func searchBtnPressed ?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading