键盘隐藏时,键盘上方的工具栏不会隐藏

时间:2020-06-22 17:22:12

标签: ios swift uibarbuttonitem uitoolbar inputaccessoryview

我在键盘上方添加了一个工具栏,以显示“完成”按钮以关闭键盘。我已经在登录屏幕上添加了它。当显示键盘时,我点击“已保存的密码”图标以选择已保存的密码,键盘将隐藏,但工具栏不会隐藏。工具栏位于屏幕底部,然后在键盘再次显示时随键盘向上移动。看起来不好。

如何修复它,以使工具栏不会自己显示,而只能通过键盘显示/隐藏?

override func viewDidLoad() {
    super.viewDidLoad()
    self.emailTextField.addDoneButton(title: "Done", target: self, selector: #selector(tapDone(sender:)))
    self.passwordTextField.addDoneButton(title: "Done", target: self, selector: #selector(tapDone(sender:)))
}

@objc func tapDone(sender: Any) {
    self.view.endEditing(true)
}

extension UITextField {
    
    // Add done button above keyboard
    func addDoneButton(title: String, target: Any, selector: Selector) {
        let toolBar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 44.0)))
        
        let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let barButton = UIBarButtonItem(title: title, style: .plain, target: target, action: selector)
        barButton.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.main, NSAttributedString.Key.foregroundColor : UIColor.red], for: [])
        toolBar.setItems([flexible, barButton], animated: false)
        self.inputAccessoryView = toolBar
    }
}

2 个答案:

答案 0 :(得分:0)

我个人以不同的方式处理此问题,因为我不使用任何工具栏,而是使用键盘上方的自定义视图。当我希望这些视图动画并根据键盘位置出现/消失时,我先听听键盘的变化:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardChanged(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

然后在此处手动处理键盘的当前大小和位置,如下所示:

func keyboardChanged(_ userInfo: Dictionary<AnyHashable, Any>?) {
    if let userInfo = userInfo {
        let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
        let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
        let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
        if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
            // keyboard is masked, you can mask/move your toolbar here
        } else {
            // update your toolbar visibility and/or position/constraints here using 'endFrame?.size.height'
        }

        // animate your toolbar or any other view here:
        UIView.animate(withDuration: duration,
                delay: TimeInterval(0),
                options: animationCurve,
                animations: {
                     // animate what you need here
                     self.view.layoutIfNeeded()
                },
                completion: nil)
    }
}

因此,在您的情况下,我将首先创建工具栏并将其限制在屏幕底部。然后,我将使用上面的代码来处理其位置和可见性。

然后,每当键盘位置更新时,您都可以在上面显示的键盘通知处理程序中处理工具栏(位置和可见性)。

答案 1 :(得分:-1)

可能不是这个问题的直接答案,但是我强烈建议您看一下IQKeyboardManager库。默认情况下,它是一个线性键盘处理程序,但是您可以轻松添加附件视图并且可以很好地对其进行管理

https://github.com/hackiftekhar/IQKeyboardManager