Ios 隐藏后的快速键盘高度

Ios 隐藏后的快速键盘高度,ios,swift,Ios,Swift,我正在开发一个聊天应用程序(想想whatsapp)。在一个viewcontroller中,用户将选择要发送的照片,然后在文本字段中输入消息。第一次文本字段输入随键盘高度移动时。那很好。现在,如果用户选择了错误的图像,单击“上一步”,并从图库或照片中选择了另一个图像,则文本字段不会随键盘移动 我的代码是: NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NS

我正在开发一个聊天应用程序(想想whatsapp)。在一个viewcontroller中,用户将选择要发送的照片,然后在文本字段中输入消息。第一次文本字段输入随键盘高度移动时。那很好。现在,如果用户选择了错误的图像,单击“上一步”,并从图库或照片中选择了另一个图像,则文本字段不会随键盘移动

我的代码是:

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
    }
`


我只是一个初级开发人员,正在努力解决这个问题,因为它可能是简单的

Aditya Srivastava是正确的-用UIKeyboardFrameEndUserInfoKey更改UIKeyboardFrameBeginUserInfoKey非常有效

非常感谢你

顺便说一下,我还想让UITextView向上扩展,而不是向下扩展,这样它就可以放在键盘下面了。我确实使用了这个代码片段

    var frame: CGRect = txtMessage.frame
    frame.origin.y = frame.maxY - txtMessage.contentSize.height
    frame.size.height = txtMessage.contentSize.height
    txtMessage.frame = frame

Aditya Srivastava是正确的-用UIKeyboardFrameEndUserInfoKey更改UIKeyboardFrameBeginUserInfoKey效果很好

非常感谢你

顺便说一下,我还想让UITextView向上扩展,而不是向下扩展,这样它就可以放在键盘下面了。我确实使用了这个代码片段

    var frame: CGRect = txtMessage.frame
    frame.origin.y = frame.maxY - txtMessage.contentSize.height
    frame.size.height = txtMessage.contentSize.height
    txtMessage.frame = frame

试试这个。它可能会对您有所帮助。尝试此操作您也可以尝试在通过执行view.endEditing(true)和in-ViewWillDisplay离开视图时关闭键盘。将您的文本字段设置为第一响应者。不确定键盘动画是否适合您的情况。请尝试此操作。它可能会对您有所帮助。尝试此操作您也可以尝试在通过执行view.endEditing(true)和in-ViewWillDisplay离开视图时关闭键盘。将您的文本字段设置为第一响应者。不确定键盘动画是否适合您的情况。