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

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

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

Antworte allen(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)。

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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!