ios - 用collectionview做个了日历 宽度均分 但中间总有缝隙
迷茫
迷茫 2017-04-18 09:46:00
0
3
765

用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);
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
迷茫

Two attempts:
1. You can set it where the item is set
2.flowLayout.sectionInset = UIEdgeInsetsMake(-0.01, -0.01, -0.01, -0.01); Try it

巴扎黑

I used a dirt explosion method
The gap is because the screen size divided by the number of rows cannot be divided, so I can only calculate it manually

The last line adds the width that cannot be divided from the previous one

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

Maybe it’s a decimal point problem. You need to judge the zoom ratio of the screen. When the zoom rate is 1, 1 = 1; when the zoom rate is 2; 0.5 = 1; when the zoom rate is 2.75(3), 1/2.75(3) = 1; ios screen, devices above 4s are 2x; That is, the zoom rate is 2; when the value is 0.5, the actual value is 1; the device can display it, but anything lower than 0.5 cannot be displayed. The scaling ratio of devices above plus is 2.75 (3x).

Your gap is most likely a decimal point problem.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template