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

前端 未结 10 1156
谎友^
谎友^ 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.

    0 讨论(0)
  • 2021-02-07 01:24

    I am also come across with this problem, in my case after searching any item from table view than click one of the result item it's opened details view but search bar does not disappeared. I use Nicat's answer with a little changes, here is my version:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        searchController.isActive = false
        self.searchBarCancelButtonClicked(searchController.searchBar)
    
        ...
    }
    
    0 讨论(0)
  • 2021-02-07 01:26

    When you tap the cancel button, the searchBar sees it like you've got a new thing to search, that's why if you put resignFirstResponder() on the cancelButton clicked it won't work, because after this it will call didBeginText.

    So I put a condition in didBeginText, so when the string in the search has less or 0 characters it will resign the first responder. Just like this:

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.characters.count < 1 {
            searchBar.resignFirstResponder()
        }
    }
    

    It's an easy approach and works as well. Best regards!

    0 讨论(0)
  • 2021-02-07 01:27

    For Objective-C solution

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
        if(@available(iOS 11.0, *)){
            [self.navigationController.navigationItem.searchController.searchBar resignFirstResponder];
        }
        // reload your table or whatever you want.
        // searchController is available after the iOS 11, so it will be good to use @available check
    }
    
    0 讨论(0)
  • 2021-02-07 01:28
    class MyController: UIViewController, UISearchBarDelegate {
        // Called when search bar obtains focus.  I.e., user taps 
        // on the search bar to enter text.
        func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
            searchBar.showsCancelButton = true
        }
    
        func searchBarCancelButtonClicked(searchBar: UISearchBar) {
            searchBar.text = nil
            searchBar.showsCancelButton = false
    
            // Remove focus from the search bar.
            searchBar.endEditing(true)
    
            // Perform any necessary work.  E.g., repopulating a table view
            // if the search bar performs filtering. 
        }
    }
    
    0 讨论(0)
  • 2021-02-07 01:36

    You need to implement the UISearchBarDelegate :

    class ViewController: UIViewController, UISearchBarDelegate {
        @IBOutlet weak var searchBar: UISearchBar!
    

    Set the search bar delegate to self

     override func viewDidLoad() {
            super.viewDidLoad()
    
            searchBar.showsCancelButton = true
            searchBar.delegate = self
    

    and then implement the butCancelSearchAction delegate function to do whatever you need to do to cancel the search action and reset the search text:

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        // Do some search stuff
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        // Stop doing the search stuff
        // and clear the text in the search bar
        searchBar.text = ""
        // Hide the cancel button
        searchBar.showsCancelButton = false
        // You could also change the position, frame etc of the searchBar 
    }
    
    0 讨论(0)
提交回复
热议问题