繁体   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