How To Scroll UICollectionView To End Of Footer or Header

by

Recently someone brought me a problem that needed my perspective. A vertical UICollectionView is using sections for displaying items. Requirement is to scroll to bottom of UICollectionView. A method that can be used used for scrolling to last item in UICollectionView is

scrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, animated: Bool)
Scrolls the collection view contents until the specified item is visible.

This method worked incomplete. It made collection view to last item. Isn’t that what is required? There is a custom footer at the end of section and footer needs to be visible. It did not scroll to last footer but stopped just before footer.

To find the information regarding offset on our footer view we will gather layout information for that particular supplementary view. Remember supplementary view can be either header or footer so this works for both. Here is an extension to UICollectionView




func scrollToSupplementaryView(ofKind kind: String, at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, animated: Bool) {
        self.layoutIfNeeded();
        if let layoutAttributes =  self.layoutAttributesForSupplementaryElement(ofKind: kind, at: indexPath) {
            let viewOrigin = CGPoint(x: layoutAttributes.frame.origin.x, y: layoutAttributes.frame.origin.y);
            var offset : CGPoint = self.contentOffset;
            
            switch(scrollPosition) {
            case UICollectionView.ScrollPosition.top:
                offset.y = viewOrigin.y - self.contentInset.top
                
            case UICollectionView.ScrollPosition.left:
                offset.x = viewOrigin.x - self.contentInset.left
                
            case UICollectionView.ScrollPosition.right:
                offset.x = (viewOrigin.x - self.contentInset.left) - (self.frame.size.width - layoutAttributes.frame.size.width)
                
            case UICollectionView.ScrollPosition.bottom:
                offset.y = (viewOrigin.y - self.contentInset.top) - (self.frame.size.height - layoutAttributes.frame.size.height)
                
            case UICollectionView.ScrollPosition.centeredVertically:
                offset.y = (viewOrigin.y - self.contentInset.top) -  (self.frame.size.height / 2 - layoutAttributes.frame.size.height / 2)
                
            case UICollectionView.ScrollPosition.centeredHorizontally:
                offset.x = (viewOrigin.x - self.contentInset.left) -  (self.frame.size.width / 2 - layoutAttributes.frame.size.width / 2)
            default:
                break
            }
            self.scrollRectToVisible(CGRect(origin: offset, size: self.frame.size), animated: animated)
        }
    }
    

Use it:


collectionView.scrollToSupplementaryView(ofKind: “UICollectionElementKindSectionFooter”, at: indexPath, at: .bottom, animated: true)



You might also like: AVAudioPlayer not playing any sound

You might also like: How to Get Substring With NSRange in Swift 5

You might also like: iOS UIKit Animation Equivalent in Android's Kotlin Animation

More Articles

Recommended posts