Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Eddiegooo/4244387db1e347200808c0a3e0cfc1c4 to your computer and use it in GitHub Desktop.

Select an option

Save Eddiegooo/4244387db1e347200808c0a3e0cfc1c4 to your computer and use it in GitHub Desktop.
UICollectionView 里cell 均分,不能占满屏幕的bug修改
- (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;
}
}
@Eddiegooo
Copy link
Author

当你用UICollectionView布局,四个cell并排均分宽度时候,会因为小数点问题,各个cell之间会有一点点间隙,采用这个方法,可以很好的解决这个bug,但是cell大小会有1px的误差,不影响使用的。找不到更好的办法解决,谁有更好的办法,还请回复我,O(∩_∩)O谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment