resignFirstResponder 没有按预期工作。每次按键时键盘都会弹出,而不是仅返回

Posted

技术标签:

【中文标题】resignFirstResponder 没有按预期工作。每次按键时键盘都会弹出,而不是仅返回【英文标题】:resignFirstResponder is not working as expected.the key board pop's down for every key press rather than for return only 【发布时间】:2019-10-10 09:34:52 【问题描述】:

我有 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
    

【问题讨论】:

你能给我们看看 setShadowTextField 的代码吗? 嗨,克里斯,我已经添加了 setShadowTextField 的代码 【参考方案1】:

这个问题有点不清楚,但是如果您希望在按下完成按钮时键盘关闭,您应该在您的视图控制器上实现 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 在每次按键后触发并创建您所看到的行为

【讨论】:

哇,解决了这个问题。我的错。实际上在 textFieldShouldReturn 中返回了 true。谢谢

以上是关于resignFirstResponder 没有按预期工作。每次按键时键盘都会弹出,而不是仅返回的主要内容,如果未能解决你的问题,请参考以下文章

resignFirstResponder 没有按预期工作

没有看到 UIScrollView 上的 UITextField 时 resignFirstResponder

resignFirstResponder 没有按预期工作。每次按键时键盘都会弹出,而不是仅返回

resignFirstResponder 不关闭键盘

检查现有的 TextField resignFirstResponder?

UITextField - resignFirstResponder 查询