Skip to content
Kevin Leong edited this page Oct 13, 2016 · 2 revisions

UISearchBar

Cancel button

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

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

        // Perform any necessary work.  E.g., repopulating a table view
        // if the search bar performs filtering.
    }
}

Embedding UISearchBar in Navigation Bar

References

References

Clone this wiki locally