当我在Swift上按出搜索栏时,如何关闭键盘? [英] How to make keyboard dismiss when i press out of searchbar on Swift?

查看:237
本文介绍了当我在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 work 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上按出搜索栏时,如何关闭键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆