如何在uitextfield或按钮之外点击退出键盘
Posted
技术标签:
【中文标题】如何在uitextfield或按钮之外点击退出键盘【英文标题】:How to exit keyboard on tap outside of uitextfield or button 【发布时间】:2020-07-04 18:56:35 【问题描述】:当不点击 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
抱歉代码乱七八糟,刚刚写了显示/隐藏密码代码。
【问题讨论】:
【参考方案1】:您可以使用UIGestureRecognizerDelegate
中的gestureRecognizer(_:, shouldReceive:)
方法排除对子视图的点击。
extension UIViewController: UIGestureRecognizerDelegate
func hideKeyboardWhenTappedAround()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
tap.delegate = self
view.addGestureRecognizer(tap)
@objc func dismissKeyboard()
view.endEditing(true)
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
touch.view?.isDescendant(of: view) == false // will return false if touch was received by a subview
更新:您可以使用 touch.view == view
代替 touch.view?.isDescendant(of: view) == false
。
【讨论】:
以上是关于如何在uitextfield或按钮之外点击退出键盘的主要内容,如果未能解决你的问题,请参考以下文章
在 UITextField 上点击时将 UIButton 移动到键盘上方