gpt4 book ai didi

swift - 如何创建之间没有空格的正方形网格?

转载 作者:行者123 更新时间:2023-11-30 13:49:27 25 4
gpt4 key购买 nike

我想要一个正方形网格,它们之间没有空格。目前,我使用自定义 UICollectionViewLayout:

class BoardViewLayout: UICollectionViewLayout {
override func collectionViewContentSize() -> CGSize {
return self.collectionView!.frame.size
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let boardSize = Int(sqrt(Double(self.collectionView!.numberOfItemsInSection(0))))
let index = indexPath.row

let size = self.collectionView!.frame.width / CGFloat(boardSize)
let xIndex = CGFloat(index%boardSize)
let yIndex = CGFloat(index/boardSize)

let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = CGRect(origin: CGPointMake(size*xIndex, size*yIndex), size: CGSizeMake(size, size))
return attributes
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributesArray = [UICollectionViewLayoutAttributes]()
for i in 0..<self.collectionView!.numberOfItemsInSection(0) {
attributesArray.append(self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: i, inSection: 0))!)
}
return attributesArray
}
}

它看起来像这样: screenshot of the result

我需要去掉第三行和第四行以及第三列和第四列之间的空格(参见屏幕截图)。
稍后,将会有方形图像,而不仅仅是颜色(边框不会改变),并且会有必须完美适合图像的背景(因此,如果所有正方形都被填充并且边框消失,它看起来就像一张图像)

我知道这很大程度上取决于系统如何缩放图像等,但必须有办法做到这一点。

感谢您的帮助

最佳答案

您只需将最小间距设置为 0:

- (CGFloat)collectionView:(UICollectionView *)collectionView 布局:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section {
返回0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView 布局:(UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
返回0.0;
}

关于swift - 如何创建之间没有空格的正方形网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com