Get keyboard height while being dismissed interactively with scroll
Asked Answered
H

1

6

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.

Hijacker answered 12/7, 2016 at 13:6 Comment(3)
Inside .animateWithDuration block, put your button's frame where you want to be placed at the end of the keyboard dismissal (eg, if you want it at the bottom, it would be something like button.frame.size.height = scrollView.contentSize.height). Or, you can achieve this with a constraint, that will hold the button, and modify that constraint's value (still inside the animateWithDuration and call layoutIfNeeded()) everytime keyboard is shown or dismissed.Dire
I'm already doing it, using you second option, I use my button constraint related to bottom and modify its value every time the keyboard's frame change. But my problem is related with the dismiss of the keyboard when I scroll my collection view. I would like to make the button follow the frame of the keyboard while the user is interactively dismissing it. I don't know if it is a bit strange the way I'm explaining.Hijacker
Did you have any success with this?Oogenesis
I
0

Add observer for the notification of keyboard position when keyboard will be gone.

This notification will provide you user info containing information about keyboard width and height

You should use this documentation link

Ibadan answered 12/7, 2016 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.