用collectionview做个了日历 宽度均分 但中间总有缝隙 应为选中需要改变背景颜色 而切背景是黑色 所以特别明显 请问有没有大神朋友过 或者有解决的思路
黑线如图所示
布局代码在此
float cellw =kDeviceWidth/7;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setItemSize:CGSizeMake(cellw, cellw*4/3)];//设置cell的尺寸
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
[flowLayout setHeaderReferenceSize:CGSizeMake(kDeviceWidth, 50)];
[flowLayout setMinimumInteritemSpacing:0]; //设置 y 间距
[flowLayout setMinimumLineSpacing:0]; //设置 x 间距
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置其边界
网上找到了一个重写布局方法 但是发现会叠加
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [super layoutAttributesForElementsInRect:rect];
for(int i = 1; i < [answer count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 0;
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
用了个土爆炸的方法有缝隙是应为屏幕款除以一行的个数除不尽 所以我只能手动算了
最后一行把前面除不尽的宽度加上去
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
float cellw ;
//5 .width = 320
//6 .width = 375
//6p .wdiht = 414
if (IS_IPHONE_5) {
if (indexPath.row % 7 ==0) {
cellw = 47;
}else{
cellw = 45.5;
}
}else if (IS_IPHONE_6) {
if (indexPath.row % 7 ==0) {
cellw = 54;
}else{
cellw = 53.5;
}
}else if (IS_IPHONE_6P){
if (indexPath.row % 7 ==0) {
cellw = 60;
}else{
cellw = 59;
}
}
return CGSizeMake(cellw, cellw*4/3);
}
Dua percubaan:
1. Anda boleh menetapkannya di mana item ditetapkan
2 flowLayout.sectionInset = UIEdgeInsetsMake(-0.01, -0.01, -0.01, -0.01);
Saya menggunakan kaedah letupan kotoran
Jurangnya adalah kerana saiz skrin dibahagikan dengan bilangan baris, jadi saya hanya boleh mengiranya secara manual
Baris terakhir menambah lebar yang tidak boleh dibahagikan daripada baris sebelumnya
Mungkin ini masalah titik perpuluhan. Anda perlu menilai nisbah zum skrin. Apabila kadar zum ialah 1, 1 = 1; apabila kadar zum ialah 2; Iaitu, kadar zum ialah 2; apabila nilai ialah 0.5, nilai sebenar ialah 1, tetapi apa-apa yang lebih rendah daripada 0.5 tidak boleh dipaparkan. Nisbah penskalaan peranti di atas tambah ialah 2.75 (3x).
Jurang anda berkemungkinan besar masalah titik perpuluhan.