2017-07-26 10 views
0

Desire Layout on CollectionViewカスタムレイアウト、私はトラブルSWIFT 3.0にcollectionview上のカスタムレイアウトを実現したスウィフト3

を使用。それは2日間同じようなレイアウトで検索していたが、見つけられなかった。 Pls誰も私の欲望のカスタムレイアウトを達成するために私を助けます。

+0

それは、水平スクロールまたは垂直ですか? – cole

+0

水平スクロール – bethinker

+0

コレクションセルのサイズ変更にデリゲートメソッドを使用できます。 – Prabhat

答えて

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

関連する問題