我尝试在 swift 上创建我的搜索栏,但是当我按下搜索栏时,我无法关闭屏幕上的键盘.当我尝试使用 textfield 时,它与此代码完美配合.
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 works 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.
试试这个:
self.mySearchController.searchBar.endEditing(true)
将 mySearchController 替换为您创建的控制器名称..如果您没有以编程方式创建它,而只是从库中拖动了一个搜索栏,那么 IBoutlet 您的可搜索到您的类并将其引用为:
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)
这篇关于当我在 Swift 上按下搜索栏时如何使键盘消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!