我的应用程序中有一个聊天页面,我正在使用InputAccessoryView作为文本字段。当键盘打开和关闭时,文本字段跟随键盘并且看起来很棒。问题是当键盘打开时,集合视图不会调整以允许滚动到内容的末尾。
可能导致此问题的任何想法?
func handleKeyboardWillShow(_ notification: Notification) {
let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
containerViewBottomAnchor?.constant = -keyboardFrame!.height
UIView.animate(withDuration: keyboardDuration!, animations: {
self.view.layoutIfNeeded()
}) { (Bool) in
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
func handleKeyboardWillHide(_ notification: Notification) {
let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
containerViewBottomAnchor?.constant = 0
UIView.animate(withDuration: keyboardDuration!, animations: {
self.view.layoutIfNeeded()
})
}
使集合视图和键盘协同工作是许多怪癖的痛苦。
最简单的方法是使用TPKeyboardAvoiding的Michael Tyson或查看代码以查看它在做什么。