resignFirstResponder 无法正常工作,因为 expected.the 键盘在每次按键时都会弹出,而不是仅在 return 时弹出

resignFirstResponder is not working as expected.the key board pop's down for every key press rather than for return only

我有 3 个 UITextField。当我为文本字段设置 resignFirstResponder 时,每次按键时键盘都会下降,当我们输入时键盘会弹出。这意味着每按下一个字母,键盘就会消失

我尝试如下所示为文本字段创建出口,但打印语句已执行,但当焦点丢失或移动到下一个文本字段时键盘没有消失

         @IBAction func done(_ sender: UITextField) {
                print("Text field done$$$$$$$$$$$$$$$")
                sender.endEditing(true)
                sender.resignFirstResponder()
        //        print("After Resign")
            } 

也尝试了下面的方法:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))) 
     override func viewDidLayoutSubviews() {

            super.viewDidLayoutSubviews()

            //Textfield
            let textfield = UITextField()
            textfield.setShadowTextField(username, "user-name")
            textfield.setShadowTextField(useremail, "email")
            textfield.setShadowTextField(userPhone, "phone")

            //Hide Keyboard
           textName.resignFirstResponder()
            textEmail.resignFirstResponder()
            userPhone.resignFirstResponder()
            //
            name = textName.text ?? ""
            emailphone = textEmail.text ?? ""

            //Button

            if isFirstTimeSubView == true {
                button?.setSemiButtonLeft(btnFemaleSelector, shadowViewFemale)
                button?.setGradientButton(btnFemaleSelector, startColor: "0d5e90", endColor: "8ec67d")

                button?.setSemiButtonRight(btnMaleSelector, shadowViewMale)
                button?.setGradientButton(btnMaleSelector, startColor: "FFFFFF", endColor: "FFFFFF")
                isFirstTimeSubView = false
            }


            button?.setRoundButton(btnSubmit, shadowView)
            button?.setGradientButton(btnSubmit, startColor: "0d5e90", endColor: "8ec67d")

            //textNam.sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
    //        self.userText.delegate = selftextName.resignFirstResponder()
        }
extension UITextField {
    func setShadowTextField(_ textfield: UITextField?, _ imagename: String?) {
        // set color & border
        textfield?.borderStyle = .none
        textfield?.backgroundColor = UIColor.white
        // set corner
        textfield?.layer.cornerRadius = (textfield?.frame.size.height ?? 0.0) / 2
        //set shadow
        textfield?.layer.shadowOpacity = 0.4
        textfield?.layer.shadowRadius = 15
        textfield?.layer.shadowOffset = CGSize(width: 5, height: 10)
        textfield?.layer.shadowColor = UIColor.gray.cgColor
        // set icon & Placeholder position
        let UIViewController = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: textfield?.frame.size.height ?? 0.0))
        let icon = UIImageView(image: UIImage(named: imagename ?? ""))

        icon.frame = CGRect(x: 0, y: 0, width: UIViewController.frame.size.width / 4, height: UIViewController.frame.size.height / 4)
        icon.center = UIViewController.center

        UIViewController.addSubview(icon)
        textfield?.leftView = UIViewController
        textfield?.leftViewMode = .always
    }
}

这个问题有点不清楚,但是如果你希望键盘在你按下完成按钮时下降,你应该在你的视图控制器上实现 UITextFieldDelegate 并且在 textFieldShouldReturn(_:) 期间从那里调用 resignFirstResponder 的方法:

class myVC: UIViewController, UITextFieldDelegate{    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool{
        return true
    }
}

并且不要忘记在 viewDidLoad 中为您的 VC 设置委托:

override func viewDidLoad() {
    super.viewDidLoad()
    /// Text field delegate handling
    textFieldFirstName.delegate = self
}

https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn?language=objc

通过调用 viewDidLayoutSubviews 中的 resignFirstResponder,每次调用 viewDidLayoutSubviews 时,您都将退出响应程序,即当视图控制器视图的边界发生变化时,视图会调整其子视图的位置,然后系统调用此方法。所以我假设键盘会改变边界,导致 viewDidLayoutSubviews 在每次按键后触发并创建你看到的行为