使用自定义UICollectionViewLayout的UICollectionView不是居中对齐的

时间:2015-09-04 15:43:46

标签: ios xcode swift

我有一个UICollectionView链接到自定义UICollectionViewLayout(下面的代码),它启用了水平滚动和分页。我从网上的某个地方获得了这个代码,以便在UICollectionView上正确实现分页。一切正常,但它没有正确对中。随附的是iPhone 5和iPhone 6上的截图。我尝试更改约束和UICollectionView的大小。

iPhone 5:http://i.stack.imgur.com/Yvgt1.png iPhone 6:http://i.stack.imgur.com/iTQhG.png

左边距(间距)是完美的,而右边是产生问题的边缘。

自定义UICollectionViewLayout类

class HorizontalCollectionViewLayout: UICollectionViewLayout {


    private var cellWidth = 90
    private var cellHeight = 90
     override func prepareLayout() {

    }


     override func collectionViewContentSize() -> CGSize {
        let numberOfPages = Int(ceilf(Float(cellCount) / Float(cellsPerPage)))
        let width = numberOfPages * Int(boundsWidth)
        return CGSize(width: CGFloat(width), height: boundsHeight)
    }

     override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        var allAttributes = [UICollectionViewLayoutAttributes]()

        for (var i = 0; i < cellCount; ++i) {
            let indexPath = NSIndexPath(forRow: i, inSection: 0)
            let attr = createLayoutAttributesForCellAtIndexPath(indexPath)

            allAttributes.append(attr)
        }

        return allAttributes
    }

     override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        return createLayoutAttributesForCellAtIndexPath(indexPath)
    }

   override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }

    private func createLayoutAttributesForCellAtIndexPath(indexPath:NSIndexPath)
        -> UICollectionViewLayoutAttributes {
            let layoutAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
            layoutAttributes.frame = createCellAttributeFrame(indexPath.row)
            return layoutAttributes
    }

    private var boundsWidth:CGFloat {
        return self.collectionView!.bounds.size.width
    }

    private var boundsHeight:CGFloat {
        return self.collectionView!.bounds.size.height
    }

    private var cellCount:Int {
        return self.collectionView!.numberOfItemsInSection(0)
    }

    private var verticalCellCount:Int {
        return Int(floorf(Float(boundsHeight) / Float(cellHeight)))
    }

    private var horizontalCellCount:Int {
        return Int(floorf(Float(boundsWidth) / Float(cellWidth)))
    }

    private var cellsPerPage:Int {
        return verticalCellCount * horizontalCellCount
    }

    private func createCellAttributeFrame(row:Int) -> CGRect {
        let frameSize = CGSize(width: cellHeight, height: cellWidth)
        let frameX = calculateCellFrameHorizontalPosition(row)
        let frameY = calculateCellFrameVerticalPosition(row)
        return CGRectMake(frameX, frameY, frameSize.width, frameSize.height)
    }

    private func calculateCellFrameHorizontalPosition(row:Int) -> CGFloat {
        let columnPosition = row % horizontalCellCount
        let cellPage = Int(floorf(Float(row) / Float(cellsPerPage)))
        return CGFloat(cellPage * Int(boundsWidth) + columnPosition * Int(cellWidth))
    }

    private func calculateCellFrameVerticalPosition(row:Int) -> CGFloat {
        let rowPosition = (row / horizontalCellCount) % verticalCellCount
        return CGFloat(rowPosition * Int(cellHeight))
    }

}

0 个答案:

没有答案