【问题标题】:keyboardFrameEndUserInfoKey not showing correct values?keyboardFrameEndUserInfoKey 没有显示正确的值?
【发布时间】:2018-10-16 14:59:10
【问题描述】:

我的视图底部有一个文本输入字段,我正在尝试对其进行上下动画以保持在键盘顶部。

func setupKeyboardObservers() {
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillChangeFrame), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillChangeFrame), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func handleKeyboardWillChangeFrame(notification: NSNotification) {

    let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double)

    print(keyboardFrame)
    orderDetailView?.textInputViewBottomAnchor?.constant = -keyboardFrame!.height
    UIView.animate(withDuration: keyboardDuration!) {
        self.view.layoutIfNeeded()
    }
}

OrderDetailView 是视图控制器的视图。

textinputview 是动画的部分,当键盘第一次出现时它可以正常工作,但是当我发送消息并且键盘退出第一响应者时,或者如果我通过在键盘外部单击而退出firstresponder,它不会动画回来。

当我从keyboardFrameEndUserInfoKey 打印cgrect 值时,它返回与键盘存在时相同的帧值(而不是0)。

这似乎只有在我从视图中向下拖动键盘时才能正常工作。

感谢您的帮助。

【问题讨论】:

    标签: ios swift uitextfield


    【解决方案1】:

    在您的情况下,当键盘隐藏时高度仍然非零,我认为这是您的问题。您需要将键盘框架转换为您的视图坐标系并据此设置约束。检查以下内容:

    @objc private func onKeyboardChange(notification: NSNotification) {
        guard let info = notification.userInfo else { return }
        guard let value: NSValue = info[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
        let newFrame = value.cgRectValue
    
        if let durationNumber = info[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, let keyboardCurveNumber = info[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
            let duration = durationNumber.doubleValue
            let keyboardCurve = keyboardCurveNumber.uintValue
            UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions(rawValue: keyboardCurve), animations: {
                self.updateFromKeyboardChangeToFrame(newFrame)
            }, completion: { _ in
                // After animation
            })
        } else {
            self.updateFromKeyboardChangeToFrame(newFrame)
        }
    }
    
    private func updateFromKeyboardChangeToFrame(_ keyboardFrame: CGRect) {
        let view: UIView! // Whatever view that uses bottom constraint
        let bottomConstraint: NSLayoutConstraint! // Your bottom constraint
    
        let constant = view.bounds.height-max(0, view.convert(keyboardFrame, from: nil).origin.y)
        bottomConstraint.constant = max(0, constant)
        view.layoutIfNeeded()
    }
    

    在你的情况下,你似乎使用

    let view = self.view
    let bottomConstraint = orderDetailView?.textInputViewBottomAnchor
    

    这取决于您如何定义约束,但您似乎需要使用负值作为bottomConstraint.constant = -max(0, constant)

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多