How to make keyboard dismiss when i press out of searchbar on Swift?

天大地大妈咪最大 提交于 2019-12-17 22:37:57

问题


I try to make my searchbar on swift, but i have a problem to dismiss keyboard on screen when i pressed out of searchbar. When i try with textfield it work perfectly fine with this code.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }   

It work when i press out of my textfield and then the keyboard is gone. I want to make like that with my searchbar, because when i use searchbar and use the same way like textfield, it doesn't work at all. Any reference or code is very useful for me.


回答1:


try this :

self.mySearchController.searchBar.endEditing(true)

replace mySearchController with your created controller name.. If you did not create it programmatically but instead you just dragged a search bar from library then IBoutlet your searchable to your class and reference it as:

self.mySearchBar.endEditing(true)



回答2:


Tested and working!

func searchBarSearchButtonClicked(searchBar: UISearchBar) 
{
    searchActive = false;
    self.mySearchBar.endEditing(true)
}

Edit for Swift 4.2

func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
    {
        searchActive = false
        self.searchBar.endEditing(true)
    }



回答3:


 func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        searchActive = false;
        searchProject.resignFirstResponder()
    }

This method will be invoked when user click search button on keyboard.So here we can dismiss keyboard.I think this is the right method.




回答4:


Firstly, Apple's UISearchBarDelegate is the correct solution to hide keyboard when users click a search button while UISearchBar's instance is the first responder (learn UIResponder). In short, searchBarSearchButtonClicked(_:) is what you need for this task.

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder() // hides the keyboard.
    doThingsForSearching()
}

If it doesn't work, check, does your controller conform to UISearchBarDelegate and secondly, does UISearchBarDelegate know about your class implementation (if you don't quite understand what am I talking about, you should learn delegation pattern starting to read here):

class YourAwesomeViewController: UIViewController, UISearchBarDelegate { // pay attention here

    @IBOutlet weak var yourSearchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.yourSearchBar.delegate = self // and it's important too
    }
}

Further, if you need to hide the keyboard touching outside of search bar without touching the search button (the user may change his mind to search something), UITapGestureRecognizer is a simple way too to deal with that.

  1. Ctrl-drag a Tap Gesture Recognizer from the Object Library to your View Controller.

  1. Ctrl-drag the recently added Tap Gesture Recognizer from the document outline in the storyboard to your class implementation as IBAction.

  1. Finally, write a code:

@IBAction func tapToHideKeyboard(_ sender: UITapGestureRecognizer) { self.yourSearchBar.resignFirstResponder() }

Also, don't forget to create @IBOutlet for the search bar to have an access inside your class implementation.

Both variants above work well in my project.




回答5:


I found it easier and simplier to use Table View for dismissal. (If you're using table view)

Swift 4:

self.tableView.keyboardDismissMode = .onDrag



回答6:


Swift 4+:

You can try, creating a tap gesture and add in the self.view

let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(sender:)))
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.isEnabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(singleTapGestureRecognizer)

and in selector func you call self.searchBar.resignFirstResponder

@objc func singleTap(sender: UITapGestureRecognizer) {
    self.searchBar.resignFirstResponder()
}



回答7:


we can do this with following methods

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchBar.showsCancelButton = true;
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        searchBar.showsCancelButton = false;
    }



回答8:


You can use a general UIViewController extension 1. Just make extension file and paste following code snippet

Swift 4

extension UIViewController {

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

    @objc func dismissKeyboard(_ sender: UITapGestureRecognizer) {
        view.endEditing(true)

        if let nav = self.navigationController {
            nav.view.endEditing(true)
        }
    }
 }
  1. Now call hideKeyboardWhenTappedAround() from viewDidLoad method



回答9:


class MaCaveViewController: UIViewController, UISearchBarDelegate {

    @IBOutlet weak var SearchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        SearchBar.delegate = self
    }

    // When button "Search" pressed
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
        print("end searching --> Close Keyboard")
        self.SearchBar.endEditing(true)
    }
}

This works very well for me.




回答10:


This works for me in Swift 4

   func searchBarSearchButtonClicked(_ searchBar: UISearchBar){

    self.searchBar.endEditing(true)

    }


来源:https://stackoverflow.com/questions/29925373/how-to-make-keyboard-dismiss-when-i-press-out-of-searchbar-on-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!