Newbie Questions Newbie Questions - 27 days ago 12
iOS Question

Dismiss Keyboard On drag

I would like to dismiss the keyboard on swipe/drag after i search for results in my

searchController
.

I tried doing it through the
main.storyboard
but it didn't help.

if i search and get many results i would like scroll down and then the keyboard will
dismiss
and let me see my results better.If anyone can help that would be great, Thanks ahead !

Right now, When i have my results as seen in the picture the keyboard stays when i scroll down and its kind of annoying.

I am using Swift 2.3 and Xcode 8.1

enter image description here

Here is my searchController setup just in case :

// MARK : Search ! //

var searchController : UISearchController!

var resultsController = UITableViewController()

override func viewDidLoad() {
super.viewDidLoad()

definesPresentationContext = true
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = true
self.searchController.searchBar.sizeToFit()
self.searchController.searchBar.barTintColor = UIColor.blackColor()
self.searchController.searchBar.endEditing(true)
self.searchController.searchBar.placeholder = "חפש ברים"
self.searchController.hidesNavigationBarDuringPresentation = false
////// MARK: END Of Search //////
}


Edit:Works but has a tiny issue



The last result's Cell is being cut in half! :

enter image description here

Answer

Implement the scroll view delegate method (scrollViewWillBeginDragging). In this method resignFirstResponder your search bar. You can also look at this SO answer.

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        searchController.searchBar.resignFirstResponder()//self.searchBar?.endEditing(true)
    }