Search code examples
swiftiglistkit

How to scroll to the bottom of a section with IGListKit


I'm trying to scroll to the bottom of a UICollectionView implemented with IGListKit. My method to do so:

func scrollToBottom(animated: Bool = true) {
    if let last = self.adapter.objects().last {
        self.adapter.scroll(
            to: last,
            supplementaryKinds: nil,
            scrollDirection: UICollectionViewScrollDirection.vertical,
            scrollPosition: UICollectionViewScrollPosition.bottom,
            animated: animated)
    }
}

This does scroll to the last item, however, it did not scroll to the bottom of the last object.

The actual result:

The actual result

The desired result:

The desired result

Any suggestions how to get my desired result?


Solution

  • Solved it by using the underlying UICollectionView and the setContentOffset method

    func scrollToBottom(animated: Bool = true) {
        let bottomOffset = CGPoint(
            x: 0,
            y: self.collectionView.contentSize.height
                - self.collectionView.bounds.size.height
                + self.collectionView.contentInset.bottom)
        self.collectionView.setContentOffset(bottomOffset, animated: animated)
    }