1

I am working on a chat app (think whatsapp). In one viewcontroller a user will pick a photo to send and then input their message in a textfield. The first time the textfield input moves with the keyboard height. That works fine. Now if a use chose the wrong image and clicked back and selected another image from the gallery or a photo the textfield does not move with the keyboard.

My code is:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

func keyboardWillShow(_ note: Notification) {

    if let keyboardSize = note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect {

        let keyboardHeight = keyboardSize.height
        let duration = (note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Float)
        let curve = (note.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! CInt)

        //----------------Image Attachment----------------
        let imageX = self.imgBackgroundSend.frame.origin.x
        let imageY = CGFloat(keyboardHeight)
        let imageWidth = self.imgBackgroundSend.frame.size.width
        let imageHeight = self.imgBackgroundSend.frame.size.height

        self.imgBackgroundSend.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight)

        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(CDouble(duration))
        UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(CInt(curve)))!)
        UIView.commitAnimations()

        let offsetScrollPoint = CGPoint(x: CGFloat(0), y: CGFloat(keyboardHeight))
        self.scrlViewImageText.contentOffset = offsetScrollPoint
    }

}


func keyboardWillHide(_ note: Notification) {

    if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardHeight = keyboardSize.height

        let duration = (note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double)
        let curve = (note.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! CInt)

        //----------------Image Attachment----------------
        let imageX = self.imgBackgroundSend.frame.origin.x
        let imageY = self.scrlViewImageText.center.y - (self.imgBackgroundSend.frame.size.height/2)
        print(keyboardHeight)

        let imageWidth = self.imgBackgroundSend.frame.size.width
        let imageHeight = self.imgBackgroundSend.frame.size.height

        self.imgBackgroundSend.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight)

        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(CDouble(duration))
        UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(CInt(curve)))!)
        UIView.commitAnimations()

        let offsetScrollPoint = CGPoint(x: CGFloat(0), y: CGFloat(0))
        self.scrlViewImageText.contentOffset = offsetScrollPoint
    }

`

I am only a junior dev and am struggling to figure this one out as easy as it probably is

Marcus Smith
  • 109
  • 2
  • 3
  • 10
  • Try this [link](https://stackoverflow.com/questions/45689664/ios-11-keyboard-height-is-returning-0-in-keyboard-notification/45689725#45689725). It may help you. – Aditya Srivastava Sep 27 '17 at 13:09
  • try this https://github.com/hackiftekhar/IQKeyboardManager – Himanth Sep 27 '17 at 13:11
  • You can also try to dismiss the keyboard when you navigate away from your view by doing view.endEditing(true) and then in viewWillAppear set your textfield as the first responder. Not sure if the keyboard animation would be okay in your case. – Carien van Zyl Sep 27 '17 at 13:17

1 Answers1

0

Aditya Srivastava was correct - changing the UIKeyboardFrameBeginUserInfoKey with UIKeyboardFrameEndUserInfoKey worked a treat.

Thank you so much.

As an aside - I also wanted to make the UITextView expand upwards not downwards so it does go below the keyboard. I did using this code snippet.

    var frame: CGRect = txtMessage.frame
    frame.origin.y = frame.maxY - txtMessage.contentSize.height
    frame.size.height = txtMessage.contentSize.height
    txtMessage.frame = frame
Marcus Smith
  • 109
  • 2
  • 3
  • 10