【问题标题】:How to exit keyboard on tap outside of uitextfield or button如何在uitextfield或按钮之外点击退出键盘
【发布时间】: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
        
    }
    
}

抱歉代码乱七八糟,刚刚写了显示/隐藏密码代码。

【问题讨论】:

    标签: ios swift xcode


    【解决方案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

    【讨论】: