Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in rounding off floating point value for collection view

I want to get rid of one pixel issue on my device.I am designing a calendar which looks like below

enter image description here

I am using a UICollectionViewCell for this purpose

   - (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat collectionViewWidth = (CGRectGetWidth(collectionView.frame) / 7);
    CGFloat collectionViewHeight = 48;
    return CGSizeMake(collectionViewWidth, collectionViewHeight);
}

The problem is that if I increase width even by 0.0000001 point then the number of cells draw per line is 6 instead of 7. I have tried increasing the UIEdgeInset (by 0.00001) as well but it's the same issue. The properties for flow layout are set appropriately.

 layout.minimumInteritemSpacing = 0.0f;
 layout.minimumLineSpacing = 0.0f;

How do I make cell size consistent without leaving the gap ? The actual width of the cell is 53.571428571428569 (375/7). If I round the value to 53.58 then I get the below view enter image description here

like image 407
Kunal Balani Avatar asked Jan 29 '16 22:01

Kunal Balani


2 Answers

This is a very popular problem, the fastest way to resolve it use collectionViewWidth % 7 == 0 for cell size

For example your collectionViewWidth == 320. Cell == 320/7 == 45.7142857, U simply should use collectionViewWidth == 322, and your problem are resolved, because cell == 46

That the increase wasn't noticeable collectionViewWidth U have two option

  • First, if you collectionViewWidth == screenWidth, simply align to the center and edges will hide out of screen limits

  • Second if you collectionViewWidth < screenWidth, use UIview to container for collectionView, and use clipTobounds, edges will hide out of container limits.

like image 200
Joe Hallenbeck Avatar answered Nov 16 '22 07:11

Joe Hallenbeck


To get rid of one pixel try using these delegate methods of UICollectionView:

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return -0.5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return -0.5;
}

or, alternatively:

layout.minimumInteritemSpacing = -0.5;
layout.minimumLineSpacing = -0.5;

It may happen that you would have to set one of these to 0.0, while the other to -0.5. Try playing with it.

like image 23
OlDor Avatar answered Nov 16 '22 08:11

OlDor