簡體   English   中英

鍵盤出現時如何向上移動集合視圖controller?

[英]How to move up collection view controller when keyboard appears?

我有一個在 storyboard 中制作的集合視圖和一個容器視圖,其中文本字段以編程方式限制在視圖的底部。 但是,當我顯示鍵盤時,集合視圖會隱藏在下面,我不知道該怎么做。 我看了看,大多數答案都說在鍵盤通知偵聽器中使用 self.view.frame.origin.y 但這對我不起作用。

我的鍵盤通知代碼

 @objc func keyboardWillShow(notification: NSNotification) { let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue let duration = ((notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue)? containerViewbottomAnchor..constant = -keyboardFrame.:height UIView.animate(withDuration. duration) { self.view.layoutIfNeeded() } }

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

第一個圖像是正常聊天,第二個是顯示鍵盤時可以看到的最大視圖。 容器視圖包含我以編程方式添加的文本字段和按鈕。 它們位於輸入附件視圖中,以在鍵盤出現時更新它們的位置。

你試過這個解決方案嗎? https://stackoverflow.com/a/57438748/13042987

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)


@objc func handleKeyboardWillShow(notification: Notification) {

        collectionView.scrollToItem(at: IndexPath(row: messagesList.count - 1, section: chatSection), at: .top, animated: false)
}

我設法通過更改為 UICollectionview controller class 來修復它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM