我尝试在 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 上按下搜索栏时如何使键盘消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
出现键盘时如何在iOS上调整UITextView的大小?How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
如何可靠地检测 iOS 9 上是否连接了外部键盘?How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
如何关闭 iOS 键盘?How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
是否可以在 ios 越狱后使用外部键盘模拟触摸事件Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
Android 上的数字软键盘Numeric Soft Keyboard on Android(Android 上的数字软键盘)
如何从 iOS 应用程序上的自定义键盘检索击键?How do I retrieve keystrokes from a custom keyboard on an iOS app?(如何从 iOS 应用程序上的自定义键盘检索击键?)