当我在 Swift 上按下搜索栏时如何使键盘消失?

How to make keyboard dismiss when I press out of searchbar on Swift?(当我在 Swift 上按下搜索栏时如何使键盘消失?)
本文介绍了当我在 Swift 上按下搜索栏时如何使键盘消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我尝试在 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 上按下搜索栏时如何使键盘消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

No resource found that matches the given name: attr #39;android:keyboardNavigationCluster#39;. when updating to Support Library 26.0.0(找不到与给定名称匹配的资源:attr android:keyboardNavigationCluster.更新到支持库 26.0.0 时) - IT屋-程序员软
How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
Numeric Soft Keyboard on Android(Android 上的数字软键盘)