我正在开发一个聊天应用程序(想想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
}
`
我只是一个初级开发者,并且正在努力想象这个很容易,因为它可能是
答案 0 :(得分:0)
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