繁体   English   中英

隐藏后迅速调高键盘高度

[英]swift keyboard height after it has been hidden

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

我的代码是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM