From 661f80928253d57d61c1e10ec0dfa3e165d33871 Mon Sep 17 00:00:00 2001 From: Oliver Binns Date: Sat, 13 Oct 2018 11:23:27 +0100 Subject: [PATCH 1/2] Keyboard is now automatically dismissed when an item is selected. --- README.md | 8 ++++++-- SearchTextField/Classes/SearchTextField.swift | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 83619d8..9ef55d7 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,9 @@ mySearchTextField.maxResultsListHeight = 200 mySearchTextField.highlightAttributes = [NSBackgroundColorAttributeName: UIColor.yellowColor(), NSFontAttributeName:UIFont.boldSystemFontOfSize(12)] // Handle what happens when the user picks an item. By default the title is set to the text field -mySearchTextField.itemSelectionHandler = {item, itemPosition in - mySearchTextField.text = item.title +mySearchTextField.itemSelectionHandler = {filteredResults, itemPosition in + mySearchTextField.text = filteredResults[itemPosition].title + mySearchTextField.resignFirstResponder() } // You can force the results list to support RTL languages - Default: false @@ -139,6 +140,9 @@ mySearchTextField.itemSelectionHandler = { filteredResults, itemPosition in // Do whatever you want with the picked item self.mySearchTextField.text = item.title + + // Hide the keyboard as user has finished their selection + self.resignFirstResponder() } // Define a results list header - Default: nothing diff --git a/SearchTextField/Classes/SearchTextField.swift b/SearchTextField/Classes/SearchTextField.swift index 2f6ac23..bbdc36f 100755 --- a/SearchTextField/Classes/SearchTextField.swift +++ b/SearchTextField/Classes/SearchTextField.swift @@ -595,6 +595,7 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource { public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if itemSelectionHandler == nil { self.text = filteredResults[(indexPath as NSIndexPath).row].title + self.resignFirstResponder() } else { let index = indexPath.row itemSelectionHandler!(filteredResults, index) From 09515b119ddd5c97296252d671c61890bcc38499 Mon Sep 17 00:00:00 2001 From: Oliver Binns Date: Fri, 2 Nov 2018 21:44:47 +0000 Subject: [PATCH 2/2] Added option to configure the auto-dismiss behaviour. --- SearchTextField/Classes/SearchTextField.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SearchTextField/Classes/SearchTextField.swift b/SearchTextField/Classes/SearchTextField.swift index bbdc36f..1d0f9f4 100755 --- a/SearchTextField/Classes/SearchTextField.swift +++ b/SearchTextField/Classes/SearchTextField.swift @@ -59,6 +59,9 @@ open class SearchTextField: UITextField { } } + // Specify whether the keyboard should automatically dismiss when an item is selected + open var autoDismiss = true + /// Set an array of SearchTextFieldItem's to be used for suggestions open func filterItems(_ items: [SearchTextFieldItem]) { filterDataSource = items @@ -595,7 +598,9 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource { public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if itemSelectionHandler == nil { self.text = filteredResults[(indexPath as NSIndexPath).row].title - self.resignFirstResponder() + if autoDismiss{ + self.resignFirstResponder() + } } else { let index = indexPath.row itemSelectionHandler!(filteredResults, index)