我正在创建一个聊天界面,并且像WhatsApp一样,我创建了一个“scrollToBottom”按钮,当用户将集合滚动一定距离时会显示该按钮。当键盘出现或消失时,此按钮与键盘框架完全吻合,唯一的问题是当以交互方式关闭键盘时,我无法使此按钮跟随键盘框架。仅在隐藏键盘之后,系统才会发送通知,并且按钮会更改其常数。

我已经尝试了所有键盘通知,但没有一个帮助我解决了这个问题。我需要一些及时的信息,以使按钮可以立即跟随键盘。甚至UIKeyboardWillChangeFrameNotification都没有为我工作。

NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector:#selector(self.keyboardWillShow(_:)),
                                                         name:UIKeyboardWillShowNotification,
                                                         object:nil)
NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector:#selector(self.keyboardWillHide(_:)),
                                                         name:UIKeyboardWillHideNotification,
                                                         object:nil)

private func configureConstantViewNewMessages(notification: NSNotification){
        if let userInfo = notification.userInfo {
            let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
            let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
            let convertedKeyboardEndFrame = view.convertRect(keyboardEndFrame, fromView: view.window)
            let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntValue << 16
            let animationCurve = UIViewAnimationOptions(rawValue: UInt(rawAnimationCurve))

            self.kNewMessages.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame) + 10

            UIView.animateWithDuration(animationDuration, delay: 0.0, options: [.BeginFromCurrentState, animationCurve], animations: {
                self.view.layoutIfNeeded()
                }, completion: nil)
        }
    }

使用上面的代码,我调用configureConstantViewNewMessages方法来设置按钮常量(kNewMessages)的动画,并且它可以根据键盘高度来更改其位置。

感谢您的支持,对于英语错误,我们深表歉意。

最佳答案

添加观察器,以在键盘消失时通知键盘位置。

此通知将为您提供用户信息,其中包含有关键盘宽度和高度的信息

您应该使用此文档link

关于ios - 获取键盘高度,同时与滚动交互关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38329796/

10-10 00:31
查看更多