CollectionViewController不适应使用InputAccessoryView显示的键盘

时间:2019-02-27 23:30:42

标签: ios swift uicollectionview uikeyboard inputaccessoryview

我的应用程序中有一个聊天页面,并且正在使用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()
    })
}

1 个答案:

答案 0 :(得分:0)

让收藏夹视图和键盘一起工作是很多怪癖的痛苦。

最简单的方法是使用TPKeyboardAvoidingMichael Tyson,或查看代码以查看其作用。