How to exit from the search on clicking on Cancel button?

前端 未结 10 1380
谎友^
谎友^ 2021-02-07 01:22

I have a search bar with cancel button. But when I click on Cancel button it doesn\'t close the search bar. How can I make that on click on Cancel it will return search bar to t

10条回答
  •  忘掉有多难
    2021-02-07 01:24

    Very quick and simple. Just conform to the UISearchBarDelegate protocol, and then implement this function in your class:

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
    }
    

    resign means that it will quit

    FirstResponder is the focus of the view

    In conclusion, this takes focus away from the search bar, effectively canceling it.

提交回复
热议问题