美文网首页iOS开发
CollectionView 系统自动布局cell大小的小误差处

CollectionView 系统自动布局cell大小的小误差处

作者: Eddiegooo | 来源:发表于2016-11-24 15:07 被阅读21次

话不多说,直接上干货

#pragma mark - private method
- (CGFloat)collectionViewCellWidth:(UICollectionView *)collectionView itemCountPerRow:(NSInteger)count indexPath:(NSIndexPath *)indexPath {
    // 处理collectionView小数点bug,因为collectView是智能布局,当出现小数点时会随机分配宽度
    NSInteger columnCount   = count;
    NSInteger cellWidth     = round(collectionView.width / columnCount);    // 四舍五入
    if (indexPath.row % columnCount == 0 ) {
        if (cellWidth * columnCount > collectionView.width) {
            return cellWidth - (cellWidth * columnCount - collectionView.width);
        } else {
            return collectionView.width - cellWidth * (columnCount - 1);
        }
    } else {
        return cellWidth;
    }
}```

###这样计算cell的宽度,就不会出现因为宽度小数取舍而出现一点点边距的问题了。。。记录下。

相关文章

网友评论

    本文标题:CollectionView 系统自动布局cell大小的小误差处

    本文链接:https://www.haomeiwen.com/subject/mtibpttx.html