14

I have UICollectionViewCells of different height. I want to set the top alignment of each cell in UICollectionView. Below is the attachment:

enter image description here

I want UICollectionViewCells be like:

enter image description here

Saransh Gaur
  • 193
  • 2
  • 9
  • Show us your relevant code – pkc456 Jul 11 '17 at 12:06
  • Possible duplicate of [How do you vertically align the UICollectionViewCells in a UICollectionView?](https://stackoverflow.com/questions/17033194/how-do-you-vertically-align-the-uicollectionviewcells-in-a-uicollectionview) – Varun Naharia Jul 11 '17 at 12:11
  • You need to subclass `UICollectionFlowLayout` and patch frames for cells `UICollectionViewLayoutAttributes`. – ArtFeel Oct 03 '18 at 17:43
  • 1
    Your question is duplicate of this - https://stackoverflow.com/a/23540071/4776634 – niku Nov 14 '18 at 10:36
  • 1
    Does this answer your question? [UICollection View Flow Layout Vertical Align](https://stackoverflow.com/questions/16837928/uicollection-view-flow-layout-vertical-align) – Rob Nov 30 '20 at 12:58

1 Answers1

0

You probably are asking for a layout like in pinterest. Check this tuto uicollectionview-custom-layout-tutorial-pinterest

Here is the code you need


protocol PinterestLayoutDelegate: AnyObject {
    func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat
}

class PinterestLayout: UICollectionViewLayout {
    // 1
    weak var delegate: PinterestLayoutDelegate?
    
    // 2
    private let numberOfColumns = 2
    private let cellPadding: CGFloat = 10
    
    // 3
    private var cache: [UICollectionViewLayoutAttributes] = []
    
    // 4
    private var contentHeight: CGFloat = 0
    
    private var contentWidth: CGFloat {
        guard let collectionView = collectionView else {
            return 0
        }
        let insets = collectionView.contentInset
        return collectionView.bounds.width - (insets.left + insets.right)
    }
    
    // 5
    override var collectionViewContentSize: CGSize {
        return CGSize(width: contentWidth, height: contentHeight)
    }
    
    override func prepare() {
        // 1
        guard cache.isEmpty == true, let collectionView = collectionView
        else { return }
        
        // 2
        let columnWidth = contentWidth / CGFloat(numberOfColumns)
        var xOffset: [CGFloat] = []
        for column in 0..<numberOfColumns {
            xOffset.append(CGFloat(column) * columnWidth)
        }
        var column = 0
        var yOffset: [CGFloat] = .init(repeating: 0, count: numberOfColumns)
        
        // 3
        for item in 0..<collectionView.numberOfItems(inSection: 0) {
            
            let indexPath = IndexPath(item: item, section: 0)
            
            // 4
            let photoHeight = delegate?.collectionView(
                collectionView, heightForPhotoAtIndexPath: indexPath
            ) ?? 180
            
            let height = cellPadding * 2 + photoHeight
            
            let frame = CGRect(
                x: xOffset[column],
                y: yOffset[column],
                width: columnWidth,
                height: height
            )
            
            let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
            
            // 5
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.frame = insetFrame
            cache.append(attributes)
            
            // 6
            contentHeight = max(contentHeight, frame.maxY)
            yOffset[column] = yOffset[column] + height
            
            column = column < (numberOfColumns - 1) ? (column + 1) : 0
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var visibleLayoutAttributes: [UICollectionViewLayoutAttributes] = []
        
        // Loop through the cache and look for items in the rect
        for attributes in cache {
            if attributes.frame.intersects(rect) {
                visibleLayoutAttributes.append(attributes)
            }
        }
        return visibleLayoutAttributes
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return cache[indexPath.item]
    }
}

How to use:

  1. set your collectionView layout
let layout = PinterestLayout()
layout.delegate = self
collectionView.collectionViewLayout = layout
  1. implement the delegate to get the height of your cell
  2. change in PinterestLayout.numberOfColumns and set number of colls

Refer to the tuto for more explanation

Siempay
  • 876
  • 1
  • 11
  • 32