简体   繁体   中英

Keyboard dismiss for search controller in navigation item on tap anywhere

I've got UITableViewController with UISearchController in the navigation's item title view. I want to make keyboard dismiss on tap anywhere in table view. How to make it?

This is my code so far:

extension UIViewController {

    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
    func dismissKeyboard() {
        view.endEditing(true)
    }
}

class TableViewController: UITableViewController, UISearchResultsUpdating {

    override func viewDidLoad() {
        super.viewDidLoad()
        hideKeyboardWhenTappedAround()

        let searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self

        navigationItem.titleView = searchController.searchBar
    }

    func updateSearchResults(for searchController: UISearchController) {
        //
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 40
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
        cell.textLabel?.text = "test1"
        return cell
    }
}

Your searchController isn't in ViewController view hierarchy, it's in NavigationController view hierarchy there is why your code not working.

If you change your code to this`

func dismissKeyboard() {
    navigationController?.view.endEditing(true)
}

it will work.

I ended up with very nice solution because TapGestureRecognizer is not exactly what I need. It fires only when I'm releasing finger from screen, and when I'm scrolling it not works at all. So:

import UIKit
import UIKit.UIGestureRecognizerSubclass

class KeyboardDismissalGestureRecognizer: UIGestureRecognizer {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        if self.state == .possible {
            UIApplication.shared.keyWindow?.endEditing(true)
        }
    }
}

extension UIViewController {

    func hideKeyboardWhenTouchedAround() {
        let tap = KeyboardDismissalGestureRecognizer(target: self, action: nil)
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
}

class TableViewController: UITableViewController, UISearchResultsUpdating {

    override func viewDidLoad() {
        super.viewDidLoad()
        hideKeyboardWhenTouchedAround()

        let searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        navigationItem.titleView = searchController.searchBar
    }
//.....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM