4

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:pkc456 Has Solved it.Works,but has a tiny issue

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

enter image description here

Newbie Questions
  • 463
  • 1
  • 11
  • 31

6 Answers6

15

Easy way to dismiss keyboard when touch or drag (applicable to TableView, CollectionView, ScrollView)

YanSte
  • 10,661
  • 3
  • 57
  • 53
6

As you are using a tableview u can able to dismiss keyboard while dragging using this property .

Swift

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag

Objective C

tableView.keyboardDismissMode =  UIScrollViewKeyboardDismissModeOnDrag
pableiros
  • 14,932
  • 12
  • 99
  • 105
mukul
  • 382
  • 1
  • 11
4

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)
    }
Community
  • 1
  • 1
pkc456
  • 8,350
  • 38
  • 53
  • 109
1

tableView.keyboardDismissMode = .onDrag

Urvish Modi
  • 1,118
  • 10
  • 11
0
  • Use tap gesture to dismiss the keyboard. Try the solution as given below:

    override func viewDidLoad() {
      super.viewDidLoad()
      let tapGesture = UITapGestureRecognizer(target: self, action:      Selector("hideKeyboard"))
      tapGesture.cancelsTouchesInView = true
      tableView.addGestureRecognizer(tapGesture)
    }
    
    func hideKeyboard() {
      tableView.endEditing(true)
    }
    
rajtharan-g
  • 432
  • 5
  • 14
  • The user said "swipe/drag" not "tap," but regardless, you need to do this: let tap = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboard)) tap.numberOfTapsRequired = 1 self.view.addGestureRecognizer(tap) – Joshua Wolff Jan 17 '19 at 03:37
0

You add this UIScrollViewDelegate

// MARK: - UIScrollViewDelegate

  func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if(velocity.y < 0){
//Scroll down 
            self.view.endEditing(true)
        }
    }
Yogendra Girase
  • 631
  • 3
  • 15