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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)
Swift - How to mutate a struct object when iterating over it(Swift - 迭代结构对象时如何对其进行变异)
How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)
Swift Cannot invoke #39;find#39; with an argument list of type #39;([Score], Score)#39; where Score is a struct(Swift 无法使用类型为“([Score],Score)的参数列表调用“find,其中 Score 是一个结构)
How to load image asynchronously with Swift using UIImageViewExtension and preventing duplicate images or wrong Images loaded to cells(如何使用 UIImageViewExtension 与 Swift 异步加载图像并防止重复图像或错误图像加载到单元格) - IT屋-程序员软