Search code examples
iosswiftuisearchbaruikeyboarduisearchcontroller

disable keyboard popup when user taps on uisearchbar


I need some help regarding this issue.

I want to disable the keyboard popup when user taps on a searbar in my app. Any one have an idea on how to do that? It would be even better if it is possible to completely disable keyboard in the app.

I am developing this app in swift for iOS btw.

I have added a searchbar in the view programmatically using the following code:

    let searchController: UISearchController!
    self.searchController = UISearchController(searchResultsController: nil)
    self.searchController.searchResultsUpdater = self
    self.searchController.dimsBackgroundDuringPresentation = false
    self.searchController.searchBar.sizeToFit()
    self.searchController.searchBar.placeholder = "Search"

Solution

  • You just need to add UIKeyboardWillShowNotification in View Did Load

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardAppears), name: UIKeyboardWillShowNotification, object: nil)
    

    Implement keyboardAppears function in Your Class and resign searchBard Responder.

    func keyboardAppears() -> Void {
        searchController.searchBar.resignFirstResponder()
    }
    

    If you want to disable keyboard for whole app you can follow this link

    Close iOS Keyboard by touching anywhere using Swift?