Swift-如何在抬起键盘时滚动collectionView的内容 [英] Swift -How to scroll the collectionView's contents when the keyboard is raised

查看:82
本文介绍了Swift-如何在抬起键盘时滚动collectionView的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是关于在键盘上下移动时升高collectionView的问题,该部分工作正常.这是关于collectionView内的scrollView不随它一起上升的.

This isn't a question about raising the collectionView when the keyboard rises and lowers, that part works fine. This is about the scrollView inside the collectionView not rising with it.

我有一个固定在屏幕底部的视图,在该视图内有一个textView.固定在视图顶部的是一个钉在屏幕顶部的collectionView

I have a view pinned to the bottom of the screen and inside that view I have a textView. Pinned to the top of the view is a collectionView that's pinned to the top of the screen

-top of viewController
    collectionView
    containerView that contains a textView
-bottom of viewController

当点击textView的textField时,我使用一条通知来检测键盘何时上下移动.它工作正常,因为collectionView像预期的那样上下波动.问题是collectionView的内容.如果collectionView上升时,collectionView的单元格填满了屏幕,则scrollVIew不会随之升高,因此单元格位于键盘后面.以下2张图片.

When the textView's textField is tapped I use a notification which detects when the keyboard rises and lowers. It works fine because the collectionView goes up and down like it's supposed to. The problem is the collectionView's contents. If the collectionView's cells fill the screen when the collectionView rises up it's scrollVIew doesn't raise with it so the the cells are behind the keyboard. 2 pics below.

发送键盘通知时,我尝试更改collectionView的contentInset,scrollIndicatorInsets,并尝试滚动到最后一个单元格,但什么也没有.细胞只是向上滚动一点.

When the keyboard notification is sent I tried changing the collectionView's contentInset, scrollIndicatorInsets, and tried scrolling to the last cell but nothing. The cells just scroll up a tad bit.

@objc fileprivate func keyboardWillShow(notification: Notification) {

    guard let keyboardDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
    guard let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }

    let keyboardFrame = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardFrame.height

    containerViewBottomAnchorConstraint?.isActive = false
    containerViewBottomAnchorConstraint = containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -keyboardHeight)
    containerViewBottomAnchorConstraint?.isActive = true

    // I tried this
    let item = tableData.count - 1
    let indexPath = IndexPath(item: item, section: 0)
    if !tableData.isEmpty {
        collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
    }

    // I tried this
    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: -keyboardHeight, right: 0)
    collectionView.contentInset = contentInsets
    collectionView.scrollIndicatorInsets = contentInsets

    UIView.animate(withDuration: keyboardDuration, animations: {
        self.view.layoutIfNeeded()
    }) { [weak self](_) in
        self?.autoScrollToLastCell() // I tried this
    }
}

@objc fileprivate func keyboardWillHide(notification: Notification) {

    guard let keyboardDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }

    containerViewBottomAnchorConstraint?.isActive = false
    containerViewBottomAnchorConstraint = containerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0)
    containerViewBottomAnchorConstraint?.isActive = true

    UIView.animate(withDuration: keyboardDuration, animations: {
        self.view.layoutIfNeeded()
    }) { [weak self](_) in
        self?.autoScrollToLastCell()
    }
}

func autoScrollToLastCell() {
    let section = 0
    let item = collectionView.numberOfItems(inSection: section) - 1
    let index = IndexPath(item: item, section: section)
    if item > 0 {
        collectionView.scrollToItem(at: index, at: .bottom, animated: true)
    }
}

键盘升起之前

键盘抬起后,collectionView向上,但其内容没有

推荐答案

修复很容易,我要做的就是将collectionView的内容插入设置为UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)

The fix was easy, all I had to do was set the collectionView's content inset to UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)

在下面,我将bottom参数设置为8,这样看来最后一个单元格的底部和containerView的顶部之间可以有填充

Below I set the bottom argument to 8 just so it appears that the last cell can have padding between its bottom and the the containerView's top

collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)

为清楚起见,设置collectionView的contentInset属性可以解决此问题.这与这8点无关.即使是0,0,0,0也可以正常工作. 8分只是为了表象/偏好.这里没有用来修复任何东西的魔术数字".

For clarity setting the collectionView’s contentInset property is what fixed this problem. It has nothing to do with the 8 points. Even it was 0,0,0,0 it worked fine. The 8 points was just for appearance/preference. There aren’t any "magic numbers" used here to fix anything.

这篇关于Swift-如何在抬起键盘时滚动collectionView的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆