Ios 使用自定义UICollectionViewLayout的UICollectionView未居中对齐

Ios 使用自定义UICollectionViewLayout的UICollectionView未居中对齐,ios,xcode,swift,Ios,Xcode,Swift,我有一个UICollectionView,它链接到一个自定义UICollectionViewLayout(下面的代码),该布局启用了水平滚动和分页。我从网上的某个地方得到了这段代码,以便在UICollectionView上正确地实现分页。除了没有正确的居中对齐外,一切正常。附件是iPhone 5和iPhone 6的屏幕截图。我已尝试更改UICollectionView的约束和大小 iPhone 5: iPhone 6: 左边的边距(间距)是完美的,而右边是造成问题的边距 自定义UICollect

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

iPhone 5: iPhone 6:

左边的边距(间距)是完美的,而右边是造成问题的边距

自定义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))
    }

}
class HorizontalCollectionViewLayout:UICollectionViewLayout{
专用变量cellWidth=90
专用单元高度=90
重写函数prepareLayout(){
}
重写func collectionViewContentSize()->CGSize{
设numberOfPages=Int(ceilf(Float(cellCount)/Float(cellsPerPage)))
让宽度=页数*Int(边界宽度)
返回CGSize(宽度:CGFloat(宽度),高度:boundsHeight)
}
重写func layoutAttributesForElementsInRect(rect:CGRect)->[AnyObject]{
var allAttributes=[UICollectionViewLayoutAttributes]()
对于(变量i=0;iUICollectionViewLayoutAttribute{
返回createLayoutAttributesForCellAtIndexPath(indexPath)
}
覆盖函数应为边界设置(新边界:CGRect)->Bool验证布局{
返回真值
}
private func createLayoutAttributesForCellAtIndexPath(indexPath:NSIndexPath)
->UICollectionViewLayoutAttribute{
让layoutAttributes=UICollectionViewLayoutAttributes(forCellWithIndexPath:indexPath)
layoutAttributes.frame=CreateCellAttributeName(indexPath.row)
返回布局属性
}
私有变量boundsWidth:CGFloat{
返回self.collectionView!.bounds.size.width
}
private-var-boundsHeight:CGFloat{
返回self.collectionView!.bounds.size.height
}
私有变量cellCount:Int{
返回self.collectionView!.numberOfItemsInSection(0)
}
私有变量verticalCellCount:Int{
返回整数(floorf(Float(boundsHeight)/Float(cellHeight)))
}
私有变量horizontalCellCount:Int{
返回Int(floorf(Float(boundsWidth)/Float(cellWidth)))
}
私有变量cellsPerPage:Int{
返回垂直单元格计数*水平单元格计数
}
私有函数createCellAttributeName(行:Int)->CGRect{
let frameSize=CGSize(宽度:cellHeight,高度:cellWidth)
设frameX=calculateCellFrameHorizontalPosition(行)
设frameY=calculateCellFrameVerticalPosition(行)
返回CGRectMake(frameX、frameY、frameSize.width、frameSize.height)
}
私有函数calculateCellFrameHorizontalPosition(行:Int)->CGFloat{
let columnPosition=行%horizontalCellCount
设cellPage=Int(floorf(Float(row)/Float(cellsPerPage)))
返回CGFloat(cellPage*Int(boundsWidth)+columnPosition*Int(cellWidth))
}
私有函数calculateCellFrameVerticalPosition(行:Int)->CGFloat{
让rowPosition=(行/水平单元格计数)%verticalCellCount
返回CGFloat(行位置*Int(单元格高度))
}
}

在哪里设置UICollectionView的宽度?从iphone5和iphone6的图片来看,它看起来可能是相同的宽度。你能在
视图中打印出UICollectionView的框架吗?你的意思是
println(self.collectionView.frame)
?是的,还是仅仅是
println(self.collectionView)
,它给出了框架iPhone 5:
(16.0,94.0,584.0,498.0)
,iPhone 6:
(16.0,94.0,584.0,498.0)
。这两个问题都是一样的,集合视图的大小是固定的(与布局无关)。我猜你在用界面生成器?如果是这样,请将集合视图的左(后)边缘固定到屏幕的右侧。自动布局将确保集合视图填充整个屏幕。如果您需要有关自动布局的帮助,google上有很多很好的教程。您在哪里设置UICollectionView的宽度?从iphone5和iphone6的图片来看,它看起来可能是相同的宽度。你能在
视图中打印出UICollectionView的框架吗?你的意思是
println(self.collectionView.frame)
?是的,还是仅仅是
println(self.collectionView)
,它给出了框架iPhone 5:
(16.0,94.0,584.0,498.0)
,iPhone 6:
(16.0,94.0,584.0,498.0)
。这两个问题都是一样的,集合视图的大小是固定的(与布局无关)。我猜你在用界面生成器?如果是这样,请将集合视图的左(后)边缘固定到屏幕的右侧。自动布局将确保集合视图填充整个屏幕。如果您需要有关自动布局的帮助,google上有很多很好的教程