aji aji - 8 months ago 52
Swift Question

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

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) {

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.

Answer Source

try this :


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: