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

喜你入骨 提交于 2020-04-08 19:02:29

问题


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 the first state?

If you have any questions - ask me, please


回答1:


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 
}



回答2:


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. 
    }
}



回答3:


Try. It works fine.

class MyViewController: UIViewController, UISearchBarDelegate {

    func searchBarTextDidBeginEditing(_searchBar: UISearchBar) {
        searchBar.setShowsCancelButton(true, animated: true)
        //write other necessary statements
    }

    func searchBarCancelButtonClicked(_searchBar: UISearchBar) {
        searchBar.text = nil
        searchBar.setShowsCancelButton(false, animated: true)

        // Remove focus from the search bar.
        searchBar.endEditing(true) 
    }
} 



回答4:


Try this:

searchController.active = false



回答5:


You need to implement the UISearchBarDelegate.

The UISearchBarDelegate protocol defines the optional methods you implement to make a UISearchBar control functional. A UISearchBar object provides the user interface for a search field on a bar, but it’s the application’s responsibility to implement the actions when buttons are tapped. At a minimum, the delegate needs to perform the actual search when text is entered in the textField. Read this

In Objective C

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

     [self.searchDisplayController setActive:NO animated:YES];

}

In Swift :

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    self.searchDisplayController.setActive(false, animated: true)
}



回答6:


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)

    ...
}



回答7:


Swift 3

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsCancelButton = true
    return true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    self.searchBar.endEditing(true)
    searchBar.resignFirstResponder()
}

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

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    self.searchBar.endEditing(true)
    searchBar.showsCancelButton = false
    searchBar.resignFirstResponder()
}

The Cancel button appears only when the user start entering characters in the field.



回答8:


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.




回答9:


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!




回答10:


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
}


来源:https://stackoverflow.com/questions/34692277/how-to-exit-from-the-search-on-clicking-on-cancel-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!