单元格宽度和高度的集合视图内容未更改

时间:2020-01-04 13:10:18

标签: ios swift uicollectionview

我希望所有设备的UICollectionView中每行有3个单元格。我有这种流程布局。 在所有设备中,收集视图单元格的宽度和大小都可以,但是内容视图的大小不会更改。
我只添加了一个宽度和高度等于colleton视图单元格并在x,y中居中的视图。 但是此视图的大小与在集合视图xib文件中设置的大小完全相同。

class EqualWidthFlowLayout: UICollectionViewFlowLayout {

    var numberOfItemsPerRow: Int = 3 {
        didSet {
            invalidateLayout()
        }
    }


    override func prepare() {
        super.prepare()

        if let collectionView = self.collectionView {
            var newItemSize = itemSize

            // Always use an item count of at least 1
            let itemsPerRow = CGFloat(max(numberOfItemsPerRow, 1))

            // Calculate the sum of the spacing between cells
            let totalSpacing = minimumInteritemSpacing * (itemsPerRow - 1.0)

            // Calculate how wide items should be
            newItemSize.width = (collectionView.bounds.size.width - totalSpacing) / itemsPerRow

            // Use the aspect ratio of the current item size to determine how tall the items should be
            if itemSize.height > 0 {
                let itemAspectRatio = itemSize.width / itemSize.height
                newItemSize.height = newItemSize.width / itemAspectRatio
            }

            // Set the new item size
            itemSize = newItemSize
        }
    }

}

0 个答案:

没有答案
相关问题