ios - Is there a way to hide a UILabel when the user starts typing, and then make it re-appear if the user removes all input?
问题描述
I'm trying to implement a search field where you type in some characters to show "possible searches". In the beginning there is a small text underneath the textbox that says "make sure to capitalize letters". This is just a UILabel. I want to make this label "disappear" when the user STARTS to type. But if the user backspaces enough to remove all text - I want the label to re-appear again.
The textbox is just made from UITextField.
This is all stored in a view.
Does anyone have an idea of how to implement something like this?
解决方案
您可以使用像这样的选择器将目标添加到您的 textFiled 来做到这一点
- 将此目标添加到您的
viewDidLoad
方法中
yourTextFiled.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
- 在您的方法下方创建像这样的选择器方法
viewDidLoad
并设置是否 textfield.text.isEmpty 比标签像这样隐藏
@objc func textFieldDidChange(_ textField: UITextField) {
if textField.text!.isEmpty {
label.isHidden = false
} else {
label.isHidden = true
}
}
希望你得到你想要的东西!
推荐阅读
- php - 将终端进程作为后台进程运行
- excel - 有没有办法以编程方式确定是否在 VBA 中打开了错误处理?
- linux - 如何在 Linux Ubuntu 中连接以太网?
- shell - 删除所有空目录但保留根目录
- ios - MacOS CI 存档
- c# - 在 C# 中使用虚拟集合使代码更高效
- windows - 当 Windows MSI 包在磁盘上的位置与旧版本不同时,安装失败并出现错误 1603
- mysql - XAMPP:尽管端口未被阻止,但 MySql 未启动
- c# - 删除目录,但保留其所有内容,包括(子目录和文件)
- python - Carnets 应用程序的挑战无法为 statsmodel 和 SciPy 安装库(Ipad Pro 上的 jupyter)