Ich arbeite an einer Chat-App (Think WhatsApp). In einem View-Controller wählt ein Benutzer ein zu sendendes Foto aus und gibt seine Nachricht in ein Textfeld ein. Beim ersten Mal bewegt sich die Textfeldeingabe mit der Tastaturhöhe. Das funktioniert gut. Wenn nun ein Benutzer das falsche Bild auswählt und zurückklickt und ein anderes Bild aus der Galerie oder einem Foto auswählt, bewegt sich das Textfeld nicht mit der Tastatur.schnelle Tastatur Höhe, nachdem es versteckt wurde
Mein Code ist:
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
}
`
Ich bin nur ein Junior-Entwickler und habe Schwierigkeiten mit diesen ein, um herauszufinden, wie einfach, wie es wahrscheinlich
Versuchen Sie, diese [link] (https://stackoverflow.com/questions/45689664/ios-11-keyboard-height-is-returning-0-in-keyboard-notification/45689725#45689725). Es kann dir helfen. –
versuchen Sie dies https://github.com/hackiftekhar/IQKeyboardManager – Himanth
Sie können auch versuchen, die Tastatur zu verlassen, wenn Sie weg von Ihrer Ansicht navigieren, indem Sie view.endEditing (true) und dann in viewWillAppear setzen Sie Ihr Textfeld als Ersthelfer. Nicht sicher, ob die Tastaturanimation in Ihrem Fall in Ordnung wäre. –