![](/img/trans.png)
[英]How to programmatically determine the height of UIStackView after a subview has been hidden swift
[英]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.