作者:漂亮的花裙子 | 来源:互联网 | 2023-05-16 04:02
Itrytomakemysearchbaronswift,butihaveaproblemtodismisskeyboardonscreenwhenipress
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.
我试着让我的搜索栏在swift上,但是当我按下searchbar的时候,我有一个问题要把键盘放在屏幕上。当我尝试使用textfield时,它可以很好地使用这些代码。
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.
当我按下文本框时,键盘就消失了。我想在我的searchbar上这样做,因为当我使用searchbar和使用textfield一样的方式时,它根本不起作用。任何引用或代码对我都很有用。
7 个解决方案
2
Firstly, Apple's UISearchBarDelegate is the correct solution to hide keyboard when users click a search button while UISearchBar's instance is the first responder
(learn UIResponder). In short, searchBarSearchButtonClicked(_:) is what you need for this task.
首先,当用户点击搜索按钮时,苹果的UISearchBarDelegate是隐藏键盘的正确解决方案,而UISearchBar的实例是第一响应者(学习UIResponder)。简而言之,searchbarsearchbuttonclick(_:)是这个任务所需要的。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder() // hides the keyboard.
doThingsForSearching()
}
If it doesn't work, check, does your controller conform to UISearchBarDelegate
and secondly, does UISearchBarDelegate
know about your class implementation (if you don't quite understand what am I talking about, you should learn delegation pattern
starting to read here):
如果它不起作用,检查,你的控制器是否符合UISearchBarDelegate,其次,UISearchBarDelegate知道你的类实现(如果你不明白我在说什么,你应该学习授权模式开始在这里阅读):
class YourAwesomeViewController: UIViewController, UISearchBarDelegate { // pay attention here
@IBOutlet weak var yourSearchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
self.yourSearchBar.delegate = self // and it's important too
}
}
Further, if you need to hide the keyboard touching outside of search bar
without touching the search button (the user may change his mind to search something), UITapGestureRecognizer is a simple way too to deal with that.
此外,如果你需要在搜索栏外隐藏触摸键盘而不触及搜索按钮(用户可能会改变主意去搜索某个东西),UITapGestureRecognizer也是一种简单的处理方法。
Ctrl-drag
a Tap Gesture Recognizer
from the Object Library
to your View Controller.
- ctrl -拖动从对象库到视图控制器的Tap手势识别器。
Ctrl-drag
the recently added Tap Gesture Recognizer
from the document outline
in the storyboard to your class implementation as IBAction
.
- ctrl -拖动最近添加的Tap手势识别器,从故事板中的文档大纲到类实现中作为IBAction。
- Finally, write a code:
- 最后,编写代码:
@IBAction func tapToHideKeyboard(_ sender: UITapGestureRecognizer) { self.yourSearchBar.resignFirstResponder() }
@IBAction func tapToHideKeyboard(_ sender: UITapGestureRecognizer) {self.yourSearchBar.resignFirstResponder()}
Also, don't forget to create @IBOutlet
for the search bar to have an access inside your class implementation.
另外,不要忘记为搜索栏创建@IBOutlet,以便在类实现中具有访问权限。
Both variants above work well in my project.
以上两种变体在我的项目中都运行良好。