【问题标题】:CollectionViewController not adjusting for keyboard showing with InputAccessoryViewCollectionViewController 未调整使用 InputAccessoryView 显示的键盘
【发布时间】: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


    【解决方案1】:

    让集合视图和键盘一起工作是一件很痛苦的事情,有很多怪癖。

    最简单的做法是使用TPKeyboardAvoiding by Michael Tyson 或查看代码以了解它在做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多