Search code examples
iosswiftuisearchdisplaycontroller

Start search after three letters in uisearchcontroller


I would like uisearchcontroller to start searching after I type at least three characters in search bar. So, what should I do for that ?

    func configureSearchController() {
    // Initialize and perform a minimum configuration to the search controller.
    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()

    let textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as! UITextField
    textFieldInsideSearchBar.font = UIFont(name: "Bauhaus", size: 19)


   searchController.searchBar.setImage(UIImage(named: "searchikon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal);

    // Place the search bar view to the tableview headerview.
    TableView.tableHeaderView = searchController.searchBar

Solution

  • All you need to is add the single required method for the UISearchController.

    func updateSearchResultsForSearchController(searchController: UISearchController)     {   
        if searchController.searchBar.text?.characters.count > 2 {
            // Filter your search results here
        }
    
    }