点击按钮并打开搜索栏

Tap on the button and open searchBar

我有一个实现了 searchBar 的 tableView。在此顶部,我有一个按钮,应该 open searchBar with keyboard。它应该是这样的:

因此,当 user tap on this button 时,它 should open the searchBar and user can start typing something。这是我的 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())
        })
        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
}

如果您在 func searchBtnPressed 中执行 searchBar.becomeFirstResponder() 会怎么样?