【发布时间】:2019-07-21 19:17:25
【问题描述】:
我的应用中有一个聊天页面,并且正在为文本字段使用 InputAccessoryView。当键盘打开和关闭时,文本字段跟随键盘并且看起来很棒。问题是当键盘打开时,collectionview 不会调整以允许滚动到内容的末尾。
任何想法可能导致此问题?
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()
})
}
【问题讨论】:
标签: ios swift uicollectionview uikeyboard inputaccessoryview