I tried to convert this Objective-C answer to Swift, but for it always crash on this line:
var previousLayoutAttributes: UICollectionViewLayoutAttributes = attributesToReturn[i - 1]
This is the function:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
var attributesToReturn:[UICollectionViewLayoutAttributes] = super.layoutAttributesForElementsInRect(rect) as! [UICollectionViewLayoutAttributes]
for var i = 0 ; i < attributesToReturn.count ; i++
{
var currentLayoutAttributes: UICollectionViewLayoutAttributes = attributesToReturn[i]
var previousLayoutAttributes: UICollectionViewLayoutAttributes = attributesToReturn[i - 1]
var maximumSpacing: CGFloat = 50
var origin = CGRectGetMaxX(previousLayoutAttributes.frame)
if origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize().width
{
var frame: CGRect = currentLayoutAttributes.frame
frame.origin.x = origin + maximumSpacing
currentLayoutAttributes.frame = frame
}
}
return attributesToReturn
}
I guess it's because at first there is no i - 1 object.. but how else can I do that?