简体   繁体   English

键盘出现时如何向上移动集合视图controller?

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

I have a collection view I made in storyboard and a container view with text field constrained to the bottom of the view programmatically.我有一个在 storyboard 中制作的集合视图和一个容器视图,其中文本字段以编程方式限制在视图的底部。 However, when I show the keyboard the collection view stays hidden underneath and I'm not sure how to.但是,当我显示键盘时,集合视图会隐藏在下面,我不知道该怎么做。 I looked and most answers say to use self.view.frame.origin.y in the keyboard notification listener but that's hasn't worked for me.我看了看,大多数答案都说在键盘通知侦听器中使用 self.view.frame.origin.y 但这对我不起作用。

my keyboard notification code我的键盘通知代码

 @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() } }

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

first image is the normal chat and second is the max view you ca see when the keyboard is shown.第一个图像是正常聊天,第二个是显示键盘时可以看到的最大视图。 the container view contains the textfield and button which I added programmatically.容器视图包含我以编程方式添加的文本字段和按钮。 they are within an input accessory view to update their positions when the keyboard comes up.它们位于输入附件视图中,以在键盘出现时更新它们的位置。

Have you tried this solution?你试过这个解决方案吗? https://stackoverflow.com/a/57438748/13042987 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)
}

I managed to fix it by changing to a UICollectionview controller class我设法通过更改为 UICollectionview controller class 来修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM