Я знаю, что для этой вещи будет много уроков, но я действительно застрял в какой-то позиции для макета ниже.Здесь я помещаю код, который я пробую.
override func prepare() {
// 1. Only calculate once
guard cache.isEmpty == true, let collectionView = collectionView else {
return
}
// 2. Pre-Calculates the X Offset for every column and adds an array to increment the currently max Y Offset for each column
let columnWidth = contentWidth / CGFloat(numberOfColumns)
var xOffset = [CGFloat]()
for column in 0 ..< numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth)
}
var column = 0
var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
// 3. Iterates through the list of items in the first section
for section in 0 ..< collectionView.numberOfSections{
for item in 0 ..< collectionView.numberOfItems(inSection: section) {
let indexPath = IndexPath(item: item, section: section)
// 4. Asks the delegate for the height of the picture and the annotation and calculates the cell frame.
let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)
let height = cellPadding * 2 + photoHeight
var frame : CGRect!
if item % 2 == 0
{
frame = CGRect(x: xOffset[column], y: yOffset[1], width: columnWidth, height: height)
}
else {
frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
}
let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
// 5. Creates an UICollectionViewLayoutItem with the frame and add it to the cache
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = insetFrame
cache.append(attributes)
// 6. Updates the collection view content height
contentHeight = max(contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + height
column = column < (numberOfColumns - 1) ? (column + 1) : 0
}
}
}
, но этот код не дал мне желаемого результата.