OGeek|极客世界-中国程序员成长平台

标题: ios - UIView 在显示键盘时出现,但在我开始输入时消失(swift)? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:24
标题: ios - UIView 在显示键盘时出现,但在我开始输入时消失(swift)?

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

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

这很好用,但是一旦我开始输入 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 在显示时绑定(bind)到键盘:

enter image description here

输入时 UIView 消失(我怀疑在键盘后面到其原始位置):

enter image description here



Best Answer-推荐答案


不要为 UIView 本身添加扩展名。只需将您的 textBox View 添加为 subview ,并将 View 的底部约束设置为根据键盘的高度进行更新。如果存在键盘,则将常量设置为键盘高度的负值,否则将其设置为零。

    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)
        }
    }

关于ios - UIView 在显示键盘时出现,但在我开始输入时消失(swift)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604788/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4