用collectionview做个了日历 宽度均分 但中间总有缝隙

用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);
}
阅读 5.4k
3 个回答

两种尝试:
1.可以在item设置的地方 在设置一下
2.flowLayout.sectionInset = UIEdgeInsetsMake(-0.01, -0.01, -0.01, -0.01); 试一下

用了个土爆炸的方法
有缝隙是应为屏幕款除以一行的个数除不尽 所以我只能手动算了

最后一行把前面除不尽的宽度加上去

- (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);
}

可能是小数点的问题哦。 你需要判断屏幕的缩放率。 缩放率 为 1 的时候 1 = 1 ; 缩放率 为 2的时候; 0.5 = 1 ; 缩放率 为 2.75(3) 的 时候 1/2.75(3) = 1; ios 屏幕, 4s 以上的设备都是 2x;也就是缩放率为2;当 取值0.5 的时候,实际取值为1; 设备可以显示, 低于 0.5 的就不能显示了。 plus 以上的设备缩放率 都是 2.75(3x)。

你的间隙很有可能就是小数点的问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题