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:

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

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

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.text = ""

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

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 ?

Leave a Reply