2017-07-26 59 views

回答

0

有很多方法可以做,並有許多第三方庫幫助。但我懷疑你不知道如何在單元之間做佈局和間距。

這裏是最好的幫助..只是玩價值觀以獲得類似的佈局。

override func prepareLayout() { 
// 1 
if cache.isEmpty { 
// 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](count: numberOfColumns, repeatedValue: 0) 

// 3 
for item in 0 ..< collectionView!.numberOfItemsInSection(0) { 

    let indexPath = NSIndexPath(forItem: item, inSection: 0) 

    // 4 
    let width = columnWidth - cellPadding * 2 
    let photoHeight = delegate.collectionView(collectionView!, heightForPhotoAtIndexPath: indexPath, 
     withWidth:width) 
    let annotationHeight = delegate.collectionView(collectionView!, 
     heightForAnnotationAtIndexPath: indexPath, withWidth: width) 
    let height = cellPadding + photoHeight + annotationHeight + cellPadding 
    let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height) 
    let insetFrame = CGRectInset(frame, cellPadding, cellPadding) 

    // 5 
    let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) 
    attributes.frame = insetFrame 
    cache.append(attributes) 

    // 6 
    contentHeight = max(contentHeight, CGRectGetMaxY(frame)) 
    yOffset[column] = yOffset[column] + height 

    column = column >= (numberOfColumns - 1) ? 0 : ++column 
} 

}}

參考UIcollectionView Layout