I'm creating a chat interface and like WhatsApp, I have created a "scrollToBottom" button, that appears when the user scrolls the collection with a certain distance. This button follows perfectly the keyboard frame when the keyboard appears and when disappears, the only problem is when the keyboard is being dismissed interactively, I can't make this button follow the keyboard frame. Only after the keyboard is hidden that the system sends the notification and the button changes its constant.
I have tried all the keyboard notifications and none of them helped me with this issue. I need something in time, that makes the button follow the keyboard without any delay. Even UIKeyboardWillChangeFrameNotification
haven't worked for me.
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)
}
}
With the code above, I call the method configureConstantViewNewMessages
to animate the constant of my button (kNewMessages) and it can change its position according to the keyboard height.
Thanks for the support and sorry for the English mistakes.