我有一个UICollectionView
,底部有一个UIView
,其主要目的是充当UIButton
和UITextView
这对夫妇的容器。
现在,我已经设置了UIKeyboardWillShow
和UIKeyboardWillHide
,它们通过为messageViewBottomConstraint
设置动画来滑动容器而起作用。
private func setKeyboardNotifications() {
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)
}
@objc func keyboardWillShow(_ notification: NSNotification) {
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
guard let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }
UIView.animate(withDuration: keyboardDuration) {
self.messageViewBottomConstraint.constant = -keyboardFrame!.height
self.view.layoutIfNeeded()
}
}
@objc func keyboardWillHide(_ notification: NSNotification) {
guard let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }
UIView.animate(withDuration: keyboardDuration) {
self.messageViewBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
我想实现UICollectionView
的向下滑动功能,该功能允许您在向下滑动时消除关键字。
override func viewDidLoad() {
super.viewDidLoad()
collectionView.keyboardDismissMode = .interactive
}
问题是我不确定如何监视键盘框架在哪里,因此我可以为其制作动画messageViewBottomConstaint
。否则,只有完全关闭键盘后,容纳UITextView
的容器才能滑下来(这不是一个很好的UX)