扩展UICollectionViewFlowLayout并设置间距

时间:2017-08-09 08:42:02

标签: ios swift uicollectionview

我正在为我的集合视图扩展public class HorizontalCollectionViewLayout : UICollectionViewFlowLayout { public var itemSize1 = CGSize(width: 0, height: 0) { didSet { invalidateLayout() } } private var cellCount = 0 private var boundsSize = CGSize(width: 0, height: 0) public override func prepare() { cellCount = self.collectionView!.numberOfItems(inSection: 0) boundsSize = self.collectionView!.bounds.size } public override var collectionViewContentSize: CGSize { let verticalItemsCount = Int(floor(boundsSize.height / itemSize1.height)) let horizontalItemsCount = Int(floor(boundsSize.width / itemSize1.width)) let itemsPerPage = verticalItemsCount * horizontalItemsCount let numberOfItems = cellCount let numberOfPages = Int(ceil(Double(numberOfItems) / Double(itemsPerPage))) var size = boundsSize size.width = CGFloat(numberOfPages) * boundsSize.width return size } public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var allAttributes = [UICollectionViewLayoutAttributes]() if cellCount > 0 { for i in 0...(cellCount-1) { let indexPath = IndexPath(row: i, section: 0) let attr = self.computeLayoutAttributesForCellAt(indexPath: indexPath) allAttributes.append(attr) } } return allAttributes } public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { return computeLayoutAttributesForCellAt(indexPath: indexPath) } public override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { return true } private func computeLayoutAttributesForCellAt(indexPath:IndexPath) -> UICollectionViewLayoutAttributes { let row = indexPath.row let bounds = self.collectionView!.bounds let verticalItemsCount = Int(floor(boundsSize.height / itemSize1.height)) let horizontalItemsCount = Int(floor(boundsSize.width / itemSize1.width)) let itemsPerPage = verticalItemsCount * horizontalItemsCount let columnPosition = row % horizontalItemsCount let rowPosition = (row/horizontalItemsCount)%verticalItemsCount let itemPage = Int(floor(Double(row)/Double(itemsPerPage))) let attr = UICollectionViewLayoutAttributes(forCellWith: indexPath) var frame = CGRect(x: 0,y: 0,width: 0,height: 0) frame.origin.x = CGFloat(itemPage) * bounds.size.width + CGFloat(columnPosition) * itemSize1.width frame.origin.y = CGFloat(rowPosition) * itemSize1.height frame.size = itemSize1 attr.frame = frame return attr } } ,但我无法设置间距。

我的课程:

viewDidLoad

我的override func viewDidLoad() { let itemWidth = 100 let itemHeight = 100 let horizontalCV = HorizontalCollectionViewLayout(); horizontalCV.minimumLineSpacing=100 horizontalCV.minimumInteritemSpacing=100 horizontalCV.itemSize1 = CGSize(width: itemWidth, height: itemHeight) instagramCollection.collectionViewLayout = horizontalCV self.instagramCollection.delegate=self self.instagramCollection.dataSource=self } 方法:

viewDidLoad

如您所见,我在horizontalCV.minimumLineSpacing=100 horizontalCV.minimumInteritemSpacing=100 方法中使用

进行设置
innerHTML

但它始终不使用0间距。

结果:

enter image description here

如何设置间距?

0 个答案:

没有答案