UIView在显示键盘时出现,但在我开始键入(快速)时消失了吗?

时间:2018-07-31 02:51:21

标签: ios swift uiview

我有一个UIView容器,其中容纳一个UITextField和两个UIButtons(请参见下图)。

我写了一个extension,它监听UIKeyboardWillShow通知并为UIView设置动画,以便在显示时绑定到键盘。

这非常有效,但是一旦我开始输入UIView,它就会消失。

扩展用法:

sendMessageView.bindToKeyboard()

UIView扩展名:

extension UIView {

    func bindToKeyboard() {

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

    }//end func

    @objc func keyboardWillShow(notification: NSNotification) {

        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double

        let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt

        let beginningFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue

        let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

        let deltaY = endFrame.origin.y - beginningFrame.origin.y


        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
            self.frame.origin.y += deltaY
        }, completion: nil)

    }//end func

}//end extension

UIView在显示时绑定到键盘:

enter image description here

键入以下内容时,UIView消失(我怀疑是在键盘后面到达其原始位置)

enter image description here

1 个答案:

答案 0 :(得分:0)

不要为UIView本身添加扩展名。只需将textBox视图添加为子视图,然后设置视图的底部约束即可根据键盘的高度进行更新。如果存在键盘,则将该常数设置为键盘高度的负值,否则将其设置为零。

    private func setupView() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardShown(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        self.view.backgroundColor = .white
        self.view.addSubview(textInputView)
        bottomConstraint = NSLayoutConstraint(item: textInputView, attribute: .bottom, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0)
        NSLayoutConstraint.activate([
            //textBox.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
            //textInputView.heightAnchor.constraint(equalToConstant: UIFont.systemFontSize+14+10),
            textInputView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
            textInputView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0)
        ])
        bottomConstraint.isActive = true
    }

    @objc private func keyBoardShown(_ notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            print(keyboardSize.height)
            bottomConstraint.constant = -keyboardSize.height
            UIView.animate(withDuration: 0.5,
                           delay: TimeInterval(0),
                           options: .beginFromCurrentState,
                           animations: { self.view.layoutIfNeeded() },
                           completion: nil)
        }
    }