【发布时间】:2025-12-09 16:15:01
【问题描述】:
当不点击 uitextfield 或不点击显示/隐藏密码按钮时,我需要能够从键盘中点击。
我以前使用此代码来执行此操作:
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
但问题在于,即使单击显示/隐藏密码眼睛图标,它也会从键盘中弹出。我用于显示/隐藏图标的代码是这样的:
extension UITextField {
func showhidepasswordbutton(image: UIImage = UIImage(systemName: "eye.slash")!) {
let button = UIButton(type: .custom)
button.setImage(image, for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.refreshforshowhide), for: .touchUpInside)
button.tintColor = .darkGray
self.rightView = button
self.rightViewMode = .always
}
@IBAction func refreshforshowhide(_ sender: Any) {
print("ok")
if self.isSecureTextEntry == true {
self.togglePasswordVisibility()
showhidepasswordbutton(image: UIImage(systemName: "eye")!)
} else if self.isSecureTextEntry == false {
self.togglePasswordVisibility()
showhidepasswordbutton(image: UIImage(systemName: "eye.slash")!)
}
}
func togglePasswordVisibility() {
let temptext = self.text
isSecureTextEntry.toggle()
self.text = ""
self.text = temptext
}
}
抱歉代码乱七八糟,刚刚写了显示/隐藏密码代码。
【问题讨论】: