您好,欢迎来到花图问答。
搜索
您的当前位置:首页CollectionView 系统自动布局cell大小的小误差处

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

来源:花图问答

话不多说,直接上干货

#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的宽度,就不会出现因为宽度小数取舍而出现一点点边距的问题了。。。记录下。

Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务