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

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

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

全部回覆 (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)。

      你的間隙很有可能就是小數點的問題。

        最新下載
        更多>
        網站特效
        網站源碼
        網站素材
        前端模板
        關於我們 免責聲明 Sitemap
        PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!