1、解決 UICollectionView的Cell 之間不應有的分割線
借鑒地址
- 項目中UICollectionViewCell的寬度為 屏幕寬度 / 7,且最小間距已設置為0 , 但是展示的cell間總有莫名其妙的分割線,解決方法:創(chuàng)建新類 繼承 UICollectionViewFlowLayout,重寫下面方法
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
//從第2個循環(huán)到最后一個
for(int i = 1; i < [attributes count]; ++i) {
//當前attributes
UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
//上一個attributes
UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
//設置最大間距,可根據需要改
NSInteger maximumSpacing = 0;
//前一個cell的最右邊
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
//如果當前一個cell的最右邊加上我們想要的間距加上當前cell的寬度依然在contentSize中,我們改變當前cell的原點位置
//不加這個判斷的后果是,UICollectionView只顯示一行,原因是下面所有cell的x值都被加到第一行最后一個元素的后面了
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return attributes;
}